Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我知道在 EF 中您可以指定 PK 由 db 生成(如 MSSQL 身份),但是这里有什么钩子吗?有没有办法像在 NHibernate 中一样使用 IIdentifierGenerator 接口插入自定义 pk 生成器?
简短的回答:没有。EF 没有那种可扩展性。
长答案:您可以通过覆盖SaveChanges或收听ObjectContext.SavingChanges、从跟踪器获取新添加的实体并分配 id 来模拟它。
SaveChanges
ObjectContext.SavingChanges