Entity Framework - retrieve ID before 'SaveChanges' inside a transaction

前端 未结 5 1277
盖世英雄少女心
盖世英雄少女心 2020-11-29 06:12

In Entity Framework - Is there any way to retrieve a newly created ID (identity) inside a transaction before calling \'SaveChanges\'?

I need the ID for a second inse

5条回答
  •  情书的邮戳
    2020-11-29 06:40

    A simple work around for this would be

    var ParentRecord = new ParentTable () {
    SomeProperty = "Some Value",
    AnotherProperty = "Another Property Value"
    };
    
    ParentRecord.ChildTable.Add(new ChildTable () {
    ChildTableProperty = "Some Value",
    ChildTableAnotherProperty = "Some Another Value"
    });
    
    db.ParentTable.Add(ParentRecord);
    
    db.SaveChanges();
    

    Where ParentTable and ChildTable are two tables connected with Foregin key.

提交回复
热议问题