Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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" ?

like image 922
matt.m.atze Avatar asked Jan 15 '23 14:01

matt.m.atze


1 Answers

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; }
like image 127
RYFN Avatar answered Apr 27 '23 07:04

RYFN