Entity Framework Foreign Key Error On Delete
Contents |
here for a quick overview of the site Help Center Detailed answers to entity framework foreign key cascade delete any questions you might have Meta Discuss the workings and
Entity Framework Delete Foreign Key Objects
policies of this site About Us Learn more about Stack Overflow the company Business Learn entity framework delete record with foreign key more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow
Entity Framework Delete Record With Foreign Key Constraint
Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Entity Framework Error Deleting Entity with Foreign Key Relationship up vote 0 down vote favorite I am having a problem deleting some entities due to a entity framework nullable foreign key error foreign key relationship. I understand the following error message and have been doing everything I can think of to delete the entities without incurring this error: The DELETE statement conflicted with the REFERENCE constraint "FK_QuizUserAnswer_QuizWithQuestion". The conflict occurred in database "SomeDatabase", table "dbo.QuizUserAnswer", column 'idQuizQuestion'. The statement has been terminated. Here is an image of the two tables in question: I am trying to delete QuizWithQuestion entities. I have made the idQuizQuestion column nullable. So, the foreign key is nullable on the QuizUserAnswer side. In the mapping files, I have specified that the relationship is optional: HasMany(t => t.QuizUserAnswers) .WithOptional(t => t.QuizWithQuestion) .HasForeignKey(t => t.idQuizQuestion); HasOptional(t => t.QuizWithQuestion) .WithMany(t => t.QuizUserAnswers) .HasForeignKey(d => d.idQuizQuestion); I have tried many, many snippets of code, so I will post the current state of the code in the hope that my intention is clear: public void RemoveQuestionsFromQuiz(IEnumerable
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site
Entity Framework Foreign Key Code First
About Us Learn more about Stack Overflow the company Business Learn more about
Entity Framework Foreign Key Fluent Api
hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join entity framework foreign key database first the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up EF code-first, cannot delete foreign http://stackoverflow.com/questions/30351762/entity-framework-error-deleting-entity-with-foreign-key-relationship key relation up vote 4 down vote favorite I have a two models shown here: public class Application { public string Name { get; set; } public virtual ICollection
SQL Server 2014 Express resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events Community https://social.msdn.microsoft.com/Forums/en-US/984d3c83-ffef-460e-848a-fb8cf993d6bc/error-on-delete-entityobjects-with-associations?forum=adodotnetentityframework Magazine Forums Blogs Channel 9 Documentation APIs and reference Dev centers https://blogs.msdn.microsoft.com/dsimmons/2010/01/30/deleting-foreign-key-relationships-in-ef4/ Retired content Samples We’re sorry. The content you requested has been removed. You’ll be auto redirected in 1 second. Ask a question Quick access Forums home Browse forums users FAQ Search related threads Remove From My Forums Answered by: Error on delete entityobjects entity framework with associations Data Platform Development > ADO.NET Entity Framework and LINQ to Entities Question 0 Sign in to vote Hi, I am getting error when I try to delete entity objects, which has child objects associated with it. here is my code: Code Snippet using (ZeusModel.AuditTrailEntities entities = new entity framework foreign ZeusModel.AuditTrailEntities()) { System.Data.Objects.ObjectQuery
30, 201013 0 0 0 I try to keep up with blog posts on the net which involve the entity framework, and this afternoon I came across this post where someone had been experimenting with EF4 and encountered some cases where things didn’t behave as they expected. I started to respond in a comment to that blog but the comment starting getting awfully long. So here’s a more complete response (which I’ll then refer to in a short comment). Please take a moment now to go read the other post for context. OK. Now that you are back, here’s my response: When you use FK-relationships (the default with EF4), there are 3 main kinds of configurations you can encounter which each have slightly different behaviors. Let me see if I can explain so that you can understand better why the EF behaves the way it does. The first case is actually what appears in the first and last examples. It's where the relationship is backed by a foreign key property on one of the entities, and where that foreign key is non-nullable but also not part of the primary key of the entity. In this case, if you delete the "dependent" entity (namely the one with the FK property-typically on the many side of a 1-many relationship), then the EF knows that the entity going away means the relationship must go away as well. If, however, you just remove the relationship (either by removing the entity from a collection on the other side or by clearing a reference navigation property or something like that), then the EF attempts to make the foreign key property null which is what will remove the relationship without removing the entity. The problem is that since the FK property is non-nullable, when you attempt to save changes, the EF will throw an exception. The EF will allow you to be temporarily in this state which makes it easier to write code where you want to switch an entity to be related to some other entity, but it won't allow you to save when you are in that state. As a side note, the EF actually goes to significant lengths to support this temporary “conceptual null” so that it’s more convenient to program against entities with non-nul