Randal Cunanan
Randal Cunanan

Reputation: 2529

Wierd Error When Adding New Entity

I have a problem when saving a new entity into the database. Whenever I save the new entity DbEntityValidationException occurs. But carefully looking at the values of the variable and the data annotations of my entity, I meet all of them.

I looked at the database and problem seems to be not on the entity I am adding, but on the previous entities saved in the database. For some reason, some of the foreign key fields which previously have values before the adding of entity are now NULL!

During debugging, I scanned all of these entities and before I call SaveChanges() method, those fields are there and not null. Then after calling SaveChanges(), the validation error occurs because I think SaveChanges() might have done something that made the other records mess up.

I don't know if this has something to do with it but when I create a new entity, I create a new instance of it and assign its property individually. Some of it's property came from entity framework and are attached. So I am assigning an attached property to a detached new object. I don't know if this caused the weird behavior. Here is an example excerpt:

Book book = new Book();
book.Title = "The FooBar";
book.Publisher = publisherRepository.Get(1); // will retrieve a Publisher object from EF

bookRepository.Add(book);

....

// under BookRepository class
public void Add(Book book)
{
   dbContext.Books.Add(book);
   // just to check if the other records are not messed up, I did this and
   // check the values in VS Debugger
   // At this point, the other records are not yet affected.
   var books = dbContext.Books.Include("Publisher").ToArray();

   dbContext.SaveChanges(); // error thrown here, 
   // checking at the validation error, other book 
   //instances have their Publisher property set to NULL!
}

Please help as I can't find other solution to this problem. I am an Entity Framework newbie myself.

Upvotes: 1

Views: 93

Answers (2)

Randal Cunanan
Randal Cunanan

Reputation: 2529

Okay, removing the [Required] attributes on navigation properties somehow removed the errors. I still fully don't get why the other rows are affected.

Upvotes: 0

Colin
Colin

Reputation: 22595

In this line var books = dbContext.Books.Include("Publisher") you are running a query on the database and telling entity framework to include all the Publishers linked to all the Books. But there are no Publishers in the database linked to books yet. So all the Books in context have Publishers set back to null.

That line has interfered with your debugging and is now adding to the confusion.

If you remove that line and go back to the original error I think you will find that the issue is to do with an attempt to add a Publisher that already exists. That happens when you do this:

book.Publisher = publisherRepository.Get(1);
bookRepository.Add(book);

The Add method marks the entire graph (i.e. both the Book and the Publisher) as Added, but the Publisher already exists in the database so that is incorrect.

You can avoid the problem by using a foreign key on the Book. Like this:

book.PublisherID = 1;
bookRepository.Add(book);

There is a comprehensive explanation of this behavior here:

Why does entity framework reinsert existing objects into my database

Upvotes: 1

Related Questions