views:

224

answers:

2

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 every time i ask a list of groups.

All i need is some way that when inserting the group fails, the event insert won't be committed in the database.

Sorry if this is a bit unclear, My English isn't really good.

+1  A: 

Creating a relationship doesn't mean you have to load the Events for the Groups - you can still query the Groups as normal.

ck
O'Rly, I thought that all the event information was loaded with every select at the groups table.
wh0emPah
No, only if you specifically select the Event information.
ck
You are completely right. I tested it out with an profiler. Thanks
wh0emPah
A: 

I am having the exact same issue. Did you find the solution?

B Seven