Johnathon64
Johnathon64

Reputation: 1320

Autogenerate ID GUID datatype Entity Framework

I have looked at this post and tried to do it this way

Autogenerate primary key (Guid) Entity Framework CTP5

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

However I am still getting the error

Identity column 'CompanyID' must be of data type int, bigint, smallint, tinyint, or decimal or numeric with a scale of 0, and constrained to be nonnullable.

This is when I run Update-Database command on the package manager console.

Upvotes: 5

Views: 1708

Answers (1)

Vsevolod Krasnov
Vsevolod Krasnov

Reputation: 1522

Had the same issue, the only thing helped me is dropping database to initial empty state, removing all migrations and creating a new one with proper GUID key:

  1. Update-Database -TargetMigration:0
  2. Remove all migrations
  3. Add-Migration InitialCreate
  4. Update-Database

I agree that this is not a best solution, but it was acceptable for me since I had started project a few hours before.

Upvotes: 3

Related Questions