Search code examples
c#asp.netasp.net-mvcentity-frameworkasp.net-identity

ASP.NET MVC Entity Framework Id cannot be null


When it comes to ASP Identity, I created a new table, custom table using code first and migration (code below). When I try to inset into that table using DbContext I have the id cannot be null error from entity framework. I tried using id with annotation as computed / identity and the error is the same.

For sure, I missed something but I cannot figure out what.

public class AspNetCountryTable
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public String Id { get; set; }
        public String CountryName { get; set; }
        public String CountryCode { get; set; }
    }

What I want to do is to have and automated generated Id (String) just the way asp identity is generating the id for the user.

Thanks!


Solution

  • Had to add at SQL level Id column as uniqueidentifier instead of string and default sql value in migration script: newsequentialid()