Representing a tinyint field as an enumeration in the Entity Framework

I have been using Linq2Sql for the past few years and am used to several functions, for example, having an int or tinyint field in the database, which I override in an DBML file with an enumeration type, so it’s convenient and convenient to run an SQL query that is directly compared to enum (see here for my question 3 years ago on this question, and answer ).

Now I'm starting a project using Entity Framework 5, and although EF seems to have gotten a lot of rights that L2S did not (like disconnect / reconnect), I see with dismay that it seems to be any simple way to change C # type such a field as enum.

Has anyone found a way to do this cleanly so that I have a query like:

 var q = entities.Things.Where(t => t.Status == TStatus.Closed); 

(and no, I don't want to apply to int or byte inline).

+4
source share
1 answer

First code

EF5 Code First supports the enum properties on .NET 4.5. The following object will create an int field in the database:

 public class Event { public int EventId { get; set; } public Status Status { get; set; } } public enum Status { Open, Closed, Pending } 

To which you could request:

 db.Events.Where(e => e.Status == Status.Pending) 

Database first

As explained in this post , here you will do the same for the First database.

Go to the model browser and create a new enumeration type, then go to any column that you want to use and change its type to the newly created enumeration.

+5
source

Source: https://habr.com/ru/post/1433590/


All Articles