0
public  class Person
{    
    [Required]
    public int? KupaId { get; set; }

    [ForeignKey("KupaId")]
    public Kupa Kupa { get; set; }

    public int? newKupaId { get; set; }

    [ForeignKey("newKupaId")]
    public Kupa NewKupa { get; set; }
}  

public class Kupa
{
    public int Id { get; set; } 

    [Index("Ix_uniqueId", IsUnique = true)]
    public int ? uniqueId { get; set; } 
}

public class MyController:Controller
{ 

    public Json EditKupa(Expression<Func<Person,bool>> criteria )
    {
     using (IKupotRepository<Person> _IPersonRepository = new SQlRepository<Person>())
    {    
    Person personToEdit=_IPersonRepository.SingleOrDefault(criteria,GetIncludeProperties());
    > //Getting the new kupa obj from db

             newKupa = GetKupa(UniqueId);
   <//changing the unique property to null
             personToEdit.Kupa.ToremId = null;
             personToEdit.Kupa.State = State.Modified;

             personToEdit.NewKupa = newKupa;
>//Assign the unique id property the value that was in the first Kupa

             personToEdit.NewKupa.ToremId = 1;
             personToEdit.newKupaId = newKupa.Id;
             personToEdit.NewKupa.State = State.Modified;

           _IPersonRepository.SaveChanges();
            }

            }

when calling saveChanges() getting an exception :unique key violation , when looking at the sql profiler i can see that EF 6 generates an update query for both the Kupa object but it tries to update the NewKupa.uniqueId before updating the Kupa.uniqueId ?

Omer David
  • 31
  • 1
  • 3
  • You don't seem to modify any `uniqueId` of any `Kupa`. What are the values of `personToEdit.Kupa.uniqueId` and `personToEdit.NewKupa.uniqueId` and how do they change during the process? – Gert Arnold Feb 14 '16 at 16:15
  • At first : personToEdit.Kupa.uniqueId=1 than modify its value to be null,personToEdit.Kupa.uniqueId=null ,than giving the newKupa personToEdit.NewKupa.uniqueId =1. – Omer David Feb 15 '16 at 17:14

1 Answers1

0

Assuming you are using SQL Server as a database server this is happening because you allow NULL values in that column and NULL = NULL is NULL so if you have multiple rows with NULL on that column you'll get the error.

To implement this in SQL statements will be like this:

CREATE UNIQUE NONCLUSTERED INDEX Idx_UniqueId_NotNull
ON Kupa(uniqueId)
WHERE uniqueId IS NOT NULL;

However, to do this in EF there is no easy way, but there is a workaround in this SO answer here.

Community
  • 1
  • 1
Mihail Stancescu
  • 4,088
  • 1
  • 16
  • 21
  • The column already defined as "WHERE uniqueId IS NOT NULL", there are already duplicates null values in that column ,the problem is that EF is trying to update the database with the new uniqueId before assign the null value to old uniqueId.... – Omer David Feb 09 '16 at 13:58
  • Anybody have answer ? – Omer David Feb 11 '16 at 13:12
  • I think that it may also be because you are not using `Include` to add the `Kupa` navigation properties. Can you show the exact code you use when the error is thrown? If EF doesn't track all entities when you modify and call `SaveChanges()` then the expression tree may be very different than what you expect. – Mihail Stancescu Feb 11 '16 at 14:52
  • I have added the exact code in the first post,when calling the repository.singleOrDefault() the is an GetIncludeProprtiy mehtod. – Omer David Feb 14 '16 at 14:24
  • anybody have answer ? – Omer David Feb 18 '16 at 17:31