Question

How to implement in EF5 a KeyTable style identity method

"Uses a table in the database to store the next Id, and advances this value every time a new block of Ids is required" from LightSpeed

I believe this is like Oracle sequences.

This feels like it should be easy (as it is in LightSpeed). This gives the ORM an easy way to do bulk inserts ie it can get 10 identities at a time, then do a bulk insert back to the db.

Am using EF5 / WCF RIA Services (latest) talking to Silverlight. The rest of the project uses bulk insert SSIS stuff.. and the SL project does some inserting. So I need to follow this convention.

Était-ce utile?

La solution

I guess the question is fundamentally more about whether Entity Framework can support this style of key generation, and a secondary part of the question is whether RIA Services would integrate OK with that.

It reminds me of the range of key generation strategies that are available in NHibernate.

Here's an answer here which suggests that EF does not have such full support as NHibernate 'out of the box':

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.

from HiLO for the Entity Framework

This answer suggests that it's not too tricky to intercept a Save (specifically an insert) in RIA Services and call a SPROC to get the new ID

you only need to call stored procedure to get a value before you are going to save the record [can put this into an] overriden SaveChanges() in your context

see https://stackoverflow.com/a/5924487/5351 in answer to What is the best way to manually generate Primary Keys in Entity Framework 4.1 Code First

and a similar answer here... https://stackoverflow.com/a/5277642/5351

Here are some findings on possibly implementing a HiLo generator (a more robust key gen pattern) with EF:

The Hi/Lo pattern describe a mechanism for generating safe-ids on the client side rather than the database. Safe in this context means without collisions. This pattern is interesting for three reasons:

  • It doesn’t break the Unit of Work pattern (check  this link and this other one)
  • It doesn’t need many round-trips as the Sequence generator in other DBMS.
  • It generates human readable identifier unlike to GUID techniques.

from http://joseoncode.com/2011/03/23/hilo-for-entityframework/

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top