HiLO for the Entity Framework
Asked Answered
C

5

17

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.

Chaeta answered 2/7, 2010 at 8:9 Comment(2)
if you're using SQL Server, consider the uniqueidentifier data type added to SQL Server.Disobey
What, exactly, is your question?Commandeer
G
5

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

Goatskin answered 24/11, 2011 at 12:34 Comment(2)
mentioned link is wrong!Electrolyte
@HamedLohi the page is not longer online, but I updated the URL to use web.archive.orgGoatskin
C
2

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.

Chaeta answered 1/1, 2011 at 15:46 Comment(1)
Yes, that's true. Good call, @martin. I was just saying that since EF isn't there yet, that's all we have available as of now.Unnecessarily
U
1

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.

Unnecessarily answered 30/12, 2010 at 23:0 Comment(0)
T
0

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.

Tedie answered 29/12, 2010 at 13:32 Comment(0)
L
0

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();
    }
}
Least answered 16/9, 2016 at 21:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.