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

前端 未结 5 1268
盖世英雄少女心
盖世英雄少女心 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:54

    @zmbq is right, you can only get the id after calling save changes.

    My suggestion is that you should NOT rely on the generated ID's of the database. The database should only a detail of your application, not an integral and unchangeable part.

    If you can't get around that issue use a GUID as an identifier due it's uniqueness. MSSQL supports GUID as a native column type and it's fast (though not faster than INT.).

    Cheers

提交回复
热议问题