matt.m.atze
matt.m.atze

Reputation: 45

How to use guid as primary key column (identity) in entity framework 5 with default value newid()?

i use Entity Framework 5.0 by "Model First". In 4.x there is a bug: the designer can't handle primary column guid with identy and defaultValue: "newid()" (source: http://leedumond.com/blog/using-a-guid-as-an-entitykey-in-entity-framework-4/)

Is it still not possible to use this in EF5? What is the problem to transform c# guid to sql "uniqueid" ?

Upvotes: 4

Views: 6012

Answers (1)

RYFN
RYFN

Reputation: 3069

This worked for me doing database-first, but may help you after you've created your database -

You can update the table and set the default value as newid() using Server Management Studio (not through EF).

Then in the EF model, update the StoreGeneratedPattern from none to Identity.

2013 edit: For completeness, in EF 5.0 Codefirst, you can specify the following data annotation:

[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public Guid TableIdColumn { get; set; }

Upvotes: 10

Related Questions