This question is locked. New answers and comments are not allowed.
Hi,
Given two tables:
Table 1:
table1_id
table1_some_column
Table 2:
table2_id
table1_id
table2_some_column
where a foreign key relationship exists via table1_id,
I want to do the following:
using (MyContext localContext = new MyContext())
{
table1_type tbl1 = new table1_type();
table2_type tbl2 = new table2_type();
tbl1.table1_some_column = some_value;
localContext.Add(tbl1);
// normally, I would want to do the following:
// tbl2.table1_id = tbl1.table1_id;
// however, tbl1.table1_id is 0 at this point
tbl2.table2_some_column = some_other_value;
localContext.Add(tbl2);
localContext.SaveChanges(); // Bombs out here since tbl2.table1_id is not valid
}
I do not want to flush or commit table 1 just to get an ID, since that will kill my ability to rollback.
Can anyone show me how to make this work within a transaction?
Thanks,
Dennis
Given two tables:
Table 1:
table1_id
table1_some_column
Table 2:
table2_id
table1_id
table2_some_column
where a foreign key relationship exists via table1_id,
I want to do the following:
using (MyContext localContext = new MyContext())
{
table1_type tbl1 = new table1_type();
table2_type tbl2 = new table2_type();
tbl1.table1_some_column = some_value;
localContext.Add(tbl1);
// normally, I would want to do the following:
// tbl2.table1_id = tbl1.table1_id;
// however, tbl1.table1_id is 0 at this point
tbl2.table2_some_column = some_other_value;
localContext.Add(tbl2);
localContext.SaveChanges(); // Bombs out here since tbl2.table1_id is not valid
}
I do not want to flush or commit table 1 just to get an ID, since that will kill my ability to rollback.
Can anyone show me how to make this work within a transaction?
Thanks,
Dennis