HiLO for the Entity Framework

2020-03-01 05:29发布

Has anyone implemented a HiLO key generator for the Entity Framework.

Read more about HiLo here: I recommend that you read http://fabiomaulo.blogspot.com/2009/02/nh210-generators-behavior-explained.html for a detailed explanation of the downsides of choosing identity.

5条回答
在下西门庆
2楼-- · 2020-03-01 06:21

Yes, someone has implemented HiLO for Entity Framework. I haven't tested it myself though: http://joseoncode.com/2011/03/23/hilo-for-entityframework/

查看更多
爷、活的狠高调
3楼-- · 2020-03-01 06:21

Entity Framework 7 has support: https://channel9.msdn.com/Blogs/Seth-Juarez/Key-Generation-Strategies-in-Entity-Framework-7

public class ExampleContext : BaseContext { 

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.ForSqlServerUseSequenceHiLo();
    }
}
查看更多
家丑人穷心不美
4楼-- · 2020-03-01 06:25

IMO Entity framework doesn't have any equivalent to NHibernate's generators. The only feature available in EF is StoreGeneratedPattern which can be set to Identity. StoreGeneratedPattern simply means that DB will assign a key and the key is returned as part of insert operation back to EF context (harder with Guids).

If you want to have some equivalent to NHibernate POID generator you have to override SaveChanges or handle SavingChanges on your ObjectContext. Then you can manually assign ID to all inserted entities from POID algorithm of your choice - but you have to implement the algorithm.

查看更多
叼着烟拽天下
5楼-- · 2020-03-01 06:26

Thanks for the answers

I think I just have to wait :-) The EF is moving in the right direction love the CTP5.

I need to comment on the answer from "Rap". Using random Guid's as indexes can really slow down the performance on the SQL Server because the indexes for each insert becomes fragmented. This I have learn from the real world, when I started working at a new company that had big problems with performance on there sql servers. moving from guid to bigint solved the problem. and there was no need to reindex all the time.

查看更多
爷的心禁止访问
6楼-- · 2020-03-01 06:32

Unfortunately, EF doesn't have anything very close to the POID generators like NHibernate does, although I hear rumors that similar capabilities will be included in the next release of EF. (What?!? Microsoft co-opting a competitor's good idea? Inconceivable!)

It wouldn't be too tough to handle the Lo part of HiLo ourselves, but the Hi part would be tricky unless we could get EF to cooperate. That would take Microsoft to refactor parts of EF which is probably why nobody has tried to do it and publish it as an open source project on github or codeplex.

In the meantime, what we've used for generating records offline and then syncing at a later time is the globally unique identifier.

var id = Guid.NewGuid();

Then assigning it to the table's id. This can be done in SaveChanges.

I know it isn't as good as HiLo but it's as close as we've come. It still has the advantages of being able to work offline and guarantee valid and unique ids.

查看更多
登录 后发表回答