Eli Eli - 11 months ago 109
C# Question

Entity Framework - Mapping DB fields to uint

I am working with a DB-first entity framework application. Using SQL Server as the DB, which does not have unsigned column types. Is it possible when generating the model from the DB to cause EF to map fields to uint instead of int? (The tables are based on external structures, and most of the fields are uint. As it stands now, I have to cast all the fields to int in order to work with EF.)

I believe this is different than How to use unsigned int / long types with Entity Framework? because I am talking about mapping in a DB-first environment, which is different from what they did there. Also, I am trying to avoid casting, if possible.

Answer Source

Unsigned types are not supported by EF.

Source: https://entityframework.codeplex.com/workitem/1489

From what I can tell there's not going to be an alternative to explicit casting. Unsigned casting is strictly enforced by C#, and conversion between the types has a possible loss of information when it comes to negative or large vales. My only suggestion is to implement a helper or extension method to perform the cast that has built in error checking.