1

In my application my user model has a foreign key to the address table. My problem is after I insert the the address into the address table I have to run a LINQ query on the whole population of the address table looking for the record I just persisted. Is there a better way to get the primary key from a record that was just persisted?

4

2 回答 2

0

我建议你将主键类型更改为 GUID,在 SQL 中为 UNIQUEIDENTIFIER。那么你就会知道你插入的id:

var id = Guid.NewGuid();
var model = new Address()
{
    Id = id,
    // ...
};
// add and save context
于 2013-10-14T09:16:34.537 回答
0

之后SaveChanges(),上下文将使用新创建的 id 更新您的实体。

于 2013-10-14T09:19:34.707 回答