ASP MVC C#: LINQ Foreign Key Constraint conflicts
- by wh0emPah
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!