EDIT: Better yet, I do this with no errors and no records inserted:
var myTopLevelEntity = _DC.TopLevelEntity.Where( p => p.ID = 1).First();
var newEntity = new Entity();
newEntity.pocofield = ""; // set all primary keys and poco fields
newEntity.TopLevelEntity = myTopLevelEntity;
_DC.Entitys.Add( newEntity);
I checked SQL server's profiler. No commands other than a select happened during th开发者_高级运维at time.
(END EDIT)
I have a set of Entities to add at once, all new. They are dependent on a record in another table. I do the following:
var myTopLevelEntity = _DC.TopLevelEntity.Where( p => p.ID = 1).First();
NewEntity has a hierarchy:
newEntity.A[].pocofields
newEntity.B[].pocofields
newEntity.C[].pocofields
newEntity.C[].D[].pocofields
So, I do this
var newEntity = new Entity();
newEntity.pocofield = ""; // various poco fields, set all keys
var A = new AEntity();
A.pocofield = ""; // various poco fields, set all keys
newEntity.As.Add( A);
var B = new BEntity();
B.pocofield = ""; // various pocos, set all keys
newEntity.Bs.Add( B);
var C = new CEntity();
C.pocofield = ""; // various, set all keys
var D = new DEntity();
D.pocofield = ""; // various, set all keys
C.Ds.Add( D);
newEntity.Cs.Add( C);
newEntity.TopLevelEntitys.Add( myTopLevelEntity);
_DC.Entitys.Add( newEntity);
At this point, no errors occur (I don't have try/catch
anywhere). No records show up in the database either. What am I doing incorrect?
I think you forgot to call _DC.SaveChanges
精彩评论