views:

26

answers:

2

Hi. I want to have unique object ids over whole database. Is there any built-in feature in EF? Or may be there are some practices/patterns?

thanks in advance.

+4  A: 

If you are using SQL Server as the backend, you could look into using a UNIQUEIDENTIFIER column rather than an IDENTITY column, which means you can use GUIDs for your IDs across the entire database.

See "GUID Property Values" in the following MSDN article for EF4

http://msdn.microsoft.com/en-us/library/dd283139.aspx

Moo
I can't use GUID. I need something more human readable, the id number is fit.
AndrewG
+1  A: 

1) Create table to store last id (int, bigint) (identifier domain)

2) Add stored procedure to retrieve the next id (int, bigint).

3) You may implement Custom Data Class EntityObject and ComplexObject (or maybe assign to every entity manualy in code)

4) Use this id in your custom class as primary key

--

If you using GUID...EF(4) has build-in feature to do this. But GUID as primary key with CLUSTERED INDEX is bad solution for the many insertion of course.

igor
EF 4 supports server-generated GUIDs which can be sequential and are fine for clustered PKs. Or use non-sequential GUIDs with an AK for the clustered index.
Craig Stuntz
thanks, Craig. will know.
igor