Assigning ids to entities with EntityFramework 4

烂漫一生 提交于 2019-12-22 04:03:47

问题


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.


回答1:


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.

public class SomeEntity
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int? SomeEntityID { get; set; }
}

Also, there is a Fluent API call to achieve a similar result:

public class YourContext : DbContext
{
    protected override void OnModelCreating( DbModelBuilder dbModelBuilder )
    {
        base.OnModelCreating( dbModelBuilder );

        var config = dbModelBuilder.Entity<SomeEntity>();
        config.Property(e => e.SomeEntityID).HasDatabaseGeneratedOption( DatabaseGeneratedOption.None);
    }
}



回答2:


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.



来源:https://stackoverflow.com/questions/5643620/assigning-ids-to-entities-with-entityframework-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!