Primary Auto Generation Key (Guid) Entity Framework CTP5

I have the following POCO class

public class Account { [Key,DatabaseGenerated(DatabaseGenerationOption.Identity)] public string AccountId { set; get; } public string FirstName { set; get; } public string LastName { set; get; } public string Email { set; get; } } 

I get the following exception when creating a database

 Identity column 'AccountId' must be of data type int, bigint, smallint, tinyint, or decimal or numeric with a scale of 0, and constrained to be nonnullable. 
+1
source share
3 answers

If you do not have:

  [Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid AccountId { set; get; } 

?

+8
source

Jeff's answer is right. Just a little tip for you. Using EF6, I wrote the following configuration to specify all the fields with the name "Id" and enter "Guid" as a person.

 modelBuilder.Properties<Guid>() .Where(info => info.Name.ToLower()== "id") .Configure(configuration => configuration.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)); 

Therefore, I do not need to write [DatabaseGenerated(DatabaseGenerationOption.Identity)] every time.

+3
source

It looks like you need to update the AccountId property from the string to one of the specified data types with an exception:

int, bigint, smallint, tinyint or decimal or numeric with a scale of 0 and limited in order to be unnecessary

Any reason the line is now?

0
source

All Articles