Search code examples
c#entity-frameworkcode-firstuint

Code First approach using uint as primary key


I was making a code first approach in MVC to generate tables. I am trying to make my Employee class's primary key to be uint to prevent negative values but apparently, I always get an exception. Is there a decorate code for an int type to prevent negative or a workaround to allow uint as primary key?

public class Employee
{
     [Key]
     [Column(Order = 0)]
     public uint EmployeeId { get; set; }

     public string FirstName { get; set; }
     public string LastName { get; set; }
     public decimal Salary { get; set; }
}

Solution

  • Take a look at this.

    Besides that, uint is not CLS compliant, so it's generally not recommended to use