1

I have a 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 the database gets created

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.
Kyle Trauberman
  • 25,414
  • 13
  • 85
  • 121
taher chhabrawala
  • 4,110
  • 4
  • 35
  • 51
  • Note: This also happens if you use the "reserved" identity column name `Id` in your model class, even if you have `[Key,DatabaseGenerated(...)]` on it - you have to "rename away" your identity property in your model to something else, as in this case, `AccountId`. – Matt Borja Sep 22 '16 at 20:11

3 Answers3

8

Shouldn't you have:

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

?

Andrius Naruševičius
  • 8,348
  • 7
  • 49
  • 78
Jeff
  • 35,755
  • 15
  • 108
  • 220
3

Jeff's answer is right. Just a little tip for you. Using EF6 I wrote next configuration in order to set all fields with name "Id" and type "Guid" as identity.

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

So I don't have to write [DatabaseGenerated(DatabaseGenerationOption.Identity)] everytime.

Artiom
  • 7,694
  • 3
  • 38
  • 45
0

Sound like you have to update your AccountId property from string to one of the mentioned datatypes in the exception:

int, bigint, smallint, tinyint, or decimal or numeric with a scale of 0, and constrained to be nonnullable

Any reason why it's now a string?

ChristiaanV
  • 5,401
  • 3
  • 32
  • 42