0

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; }
}
NoobProgger
  • 70
  • 1
  • 9
  • 1
    What's the exception? – NibblyPig Mar 14 '19 at 10:03
  • Your questuon has _nothing_ to do with MVC or Web. – H H Mar 14 '19 at 10:07
  • 1
    `uint` should be possible but it is not advised. Just use `int`. – H H Mar 14 '19 at 10:08
  • if you want higher precision than `int` then use `long` instead of `uint`. – er-sho Mar 14 '19 at 10:13
  • Unsigned integers are unsupported in many SQL databases. For example, SQL server will just use `long` to "support" `uint`. Use `int` and ensure that it is not negative in your domain code. Also, consider using just `Id`. It is in `Employee` class, so it already implies that `Id` is employee ID. – Yeldar Kurmangaliyev Mar 14 '19 at 10:18
  • the unsigned integers/signed bytes are only really in the C# language to support interop scenarios. They should never be your preference if you're picking a type *from scratch*. – Damien_The_Unbeliever Mar 14 '19 at 10:19

1 Answers1

1

Take a look at this.

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

Loxx
  • 111
  • 9