使用 EntityFramework 4 为实体分配 id
我想为我的实体实现“默认”ID 生成支持。
保存实体时,我希望 EntityFramework 仅生成实体的 id 值(如果尚未设置)。如果 ID 已经具有非空、非零值,我希望在将实体保存到数据库中时保留该实体 ID。
我正在将数据从旧数据模型(从旧数据库创建的 EntityFramework 模型)迁移到新创建的(模型优先)EntityFramework 模型。我们将旧模型称为 A,将新模型称为 T。
通常,我希望 T 实体在保存时设置其 Id(它们都是 int64),以便长期使用新模型。
目前,我根据要从中迁移的相应 A 实体的 Id 显式分配 T 实体 Id。这样迁移结果就很容易检查。
但是,尽管我可以在迁移例程中将 T 实体的 id 分配给与 A 实体相同的 id,但在保存实体后,Id 值已更改。
有没有办法覆盖 T 模型中所有实体的默认保存方法,以便仅在保存之前尚未在实体中设置 id 值时才分配它?
我在这里查看了其他一些 EntityFramework/Id 问题,但在我看来,他们都没有问同样的事情。
感谢您提供任何线索。
I'd like to implement "default" Id generation support for my entities.
When saving the entity, I'd like EntityFramework to only generate the id value for the Entity if it's not already set. If the ID already has a non-null, non-zero value, I want to have that entity ID preserved when the entity is saved in the database.
I'm migrating data from a legacy data model (EntityFramework model created from the old database) to a newly created (model-first) EntityFramework model. Let's call the old model A, and the new model T.
Typically, I'd want T entities to get their Ids set upon save (they're all int64), for the long-term use of the new model.
Currently, I'm explicitly assigning T entity Ids based on the Id of the corresponding A entity from which I'm migrating. This is so the migration results are easy to check.
However, although I can assign the id for a T entity to the same id as the A entity in my migration routine, after I save the entities the Id values have changed.
Is there a way to override the default saving method for all entities in the T model so the id value is only assigned if it's not already set in the entity before it gets saved?
I've looked at some of the other EntityFramework/Id questions here, but to my mind none of them are asking the same thing.
Thanks for any leads.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我看到您提到您正在使用模型优先。不过,我有一个 Code First 示例,可能会有所帮助。
在 T 实体上,id 属性上有一个 [Key] 属性,不是吗?如果是这样,您可以使用另一个属性 DatabaseGenelatedAttribute,强制设置身份。
此外,还有一个 Fluent API 调用可以实现类似的结果:
I see you mentioned that you are using Model First. However, I have a Code First sample that may be of some help.
On the T entity, you have a [Key] attribute on the id property, no? If so, you can use another attribute, DatabaseGeneratedAttribute, to force the identity to be set.
Also, there is a Fluent API call to achieve a similar result:
在继续搜索、阅读以及与一些比我现在更了解 EF 的朋友进行讨论之后,我认为通过当前实体框架实现实现我所寻求的唯一实用、可持续的方法是创建一个自定义实体框架提供程序,如 此博客文章。
我是否有时间来实现这个是我必须弄清楚的事情,但我想我至少会完成这个答案。自定义提供程序还允许对实体的读属性实现解密/写属性加密。
不过,我希望看到这些功能融入到 EntityFramework 的未来版本中。
After continued searching, reading, and discussions with some friends who know more about EF than I currently do, I think the only practical, sustainable way to achieve what I'm looking for with the current Entity Framework implementation is to create a custom Entity Framework provider, as discussed in this blog post.
Whether or not I'll have the time to implement this is something I'll have to figure out, but I thought I'd at least wrap up this answer. A custom provider would also allow one to implement decrypt on read/encrypt on write attributes for the entities as well.
These features are both things I hope to see baked in to a future release of EntityFramework, though.