We will have an InvalidOperationException, thrown by the PrepareToSaveChanges method of the ObjectContext. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted. When a change is made to a relationship, the related foreign-key property is set to a null value. Message: System.InvalidOperationException : The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. > : The DELETE statement conflicted with the REFERENCE.īut if we use the second approach and write this code:Įntity framework will fail with this error: > .UpdateException : An error occurred while updating the entries. DbUpdateException : An error occurred while updating the entries. We have forget the deletion of the tags and we will get an DbUpdateException, like this: Var post = await db.Posts.Include(p => p.Categories).Include(p => p.Tags).FirstOrDefaultAsync(p => p.Id = postEntity.Id) ĭb.Categories.RemoveRange(post.Categories) īetter, we have reduced the code and improved the performance by loading the entity and the relations with one shot.Īnyway what happens if we forget the deletion of one of the related entities? PrepareToSaveChanges methodĪs we have seen before, we have at least two different methods to remove the related entities with the first one, we write some code like this: We can delete some rows and load all the related entities with the include method: This is simple and a commonly used pratice, but we have an alternative. Var post = await db.Posts.FirstAsync(p => p.Id = postEntity.Id) Īwait db.SaveChangesAsync().ConfigureAwait(false) Var tags = db.Tags.Where(c => c.PostId = postEntity.Id) Var categories = db.Categories.Where(c => c.PostId = postEntity.Id) Now if we want to delete the Post entity and the related we can do this: Suppose that we have a Post table, with the related entities Categories and Tags of course the last two have a foreign key to the Post table: Hope you enjoyed this article, let me know in the comments if you’re using something similar or have any improvements on the above.Entity framework has some features and peculiarities that are useful in a lot of situations and with they can help us to do some operations in very smart and efficient mode.Īnyway in some circumstances it could surprise us with some very strange side effects or behaviors that may we don’t know we can meet one of these in the deletion of an entity and the related entities. There are others ( SaveChangesAsync) - so just make sure those are overriden too if they’re being used in the codebase. I’ve only overridden one of the SaveChanges methods. By marking the entity as unchanged, then changing one field we keep the SQL update statement small and constrained to only that field One final thing Previously I was marking the whole entity as modified, then setting the IsDeleted flag, this resulted in EF Core sending all the fields in the update statement - which is a bit too chatty. This ensures that only this update gets sent to the Db (if you examine the SQL it sends to the database, it’s a simple update, with a single Set \ = True Then we set the IsDeleted flag to true (modifying them). RemoveRange(entities) on)įor each of those entities, if they’re Soft-Deletable then we mark their state as Unchanged, this tells EF that they’re tracked, but nothing has been modified. Then we find all the entities in the context which have been marked for deletion (entities we’ve used. The override above, instructs EF to figure out what’s changed on the context (if you’ve added, modified or removed anything). Protected override void OnModelCreating ( ModelBuilder modelBuilder )
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |