ASP MVC C#: LINQ Foreign Key Constraint conflicts
Posted
by wh0emPah
on Stack Overflow
See other posts from Stack Overflow
or by wh0emPah
Published on 2010-05-12T10:54:48Z
Indexed on
2010/05/12
11:04 UTC
Read the original article
Hit count: 630
I'm having a problem with LINQ.
I have 2 tables (Parent-child relation)
Table1: Events (EventID, Description)
Table2: Groups (GroupID, EventID(FK), Description)
Now i want to create an Event an and a child.
Event e = new Event();
e.Description = "test";
Datacontext.Events.InsertOnSubmit(event)
Group g = new Group();
g.Description = "test2";
g.EventID = e.EventID;
Datacontext.Groups.InsertOnSubmit(g);
Datacontext.SubmitChanges();
When i debug, i can see that after inserting the event. the EventID has gotten a new value (auto increment).
But when Datacontext.SubmitChanges(); gets called. I get the following exception
"The INSERT statement conflicted with the FOREIGN KEY constraint ...
I know this can be solved by creating a relation in the LINQ diagram between Events and groups. And then setting the entity itself. But i don't want to load the events everytime i ask a list of groups.
All i need is some way that when inserting the group fails, the event insert won't be comitted in the database.
Sorry if this is a bit unclear, My english isn't really good. Thanks in advance!
© Stack Overflow or respective owner