Поделиться через


Enums in the Entity Designer

One of the most highly-requested features for the Entity Framework is first-class support for Enums, and we are happy to provide support for this feature in the Entity Designer in the June 2011 CTP recently released. If you haven’t checked it out already, find the bits here:

https://www.microsoft.com/download/en/details.aspx?displaylang=en&id=26660

We’d like to give you a look at the experience of developing with Enum types in the Entity Designer. If you haven’t yet, take a look at what is going on under the hood with Enums in the Entity Framework in the blog post here:

https://blogs.msdn.com/b/efdesign/archive/2011/06/29/enumeration-support-in-entity-framework.aspx

and you can follow an example of using Enums in the Walkthrough here:

https://blogs.msdn.com/b/adonet/archive/2011/06/30/walkthrough-enums-june-ctp.aspx

 

While the Walkthrough will take you briefly through the Entity Designer Enums experience, we want to make sure we show what is available. We’d also like you to use this as a way to give direct feedback to the Entity Designer team on the user experience of working with Enums. We are already working on improvements to the experience shipped in the CTP, so please let us know what you think!

 

Experience

Similar to how Complex Types are represented in the Entity Designer, the primary location to work with Enums is via the Model Browser.

 

 

 There are two major points to note about using Enums in your Entity Data Model:

1.       Enum Types are not supported as shapes on the Entity Designer diagram surface

2.       Enum Types are not created in your model via Database-First actions

a.       When you create an EDM from an existing database, Enums are not defined in your model.

b.      Update Model from Database will preserve your declaration of Enum types, but again, will not detect Enum constructs from your database.

Please let us know your feedback on the importance of these items. For example, how valuable is it to you to be able to convert reference entity types imported from Database-First into enum types?

Enum Type Dialog

 

 

Name: Name of Enum Type

Underlying Type: Valid underlying types for enum types are Int16, Int32, Int64, Byte, SByte.

IsFlags: When checked, denotes that this enum type is used as a bit field, and applies the Flags attribute to the definition of the Enum Type.

Member Name and Value (optional): Type or edit the members for the enum type and define optional values. Values must adhere to the underlying type of the enum.

Errors: Errors with your Enum Type definition will appear in the dialog in red, accompanied by a tooltip explaining what is invalid. Common errors are:

-          Not a valid enum member value: A Value entered that is invalid according to the underlying type

-          Member is duplicated: Multiple Members entered with the same name

-          Not a valid name for an enum member: A Member name entered with unsupported characters

Errors must be fixed before the Enum Type can be created or updated. We do have known issues with our error UI that we are fixing as I write! Feedback is more than welcome as to what is most helpful to you in the dialog.

Entry Points

Create new Enum Type and Edit existing Enum Type:

Context Menu on Enum Types folder or existing Enum node in Model Browser

 

Convert Property to Enum:

Context menu on a Property in an Entity Type

 

 

Converting a property to an Enum type will create a new Enum Type. To use an existing Enum Type for a property, select the property and you will find existing Enum Types available in the Property Window in the Type drop down.

Use existing Enum Type:

Type in Property Window

 

 

 

Feedback

As always, please let us know what is most valuable to you for using Enum Types in the Entity Designer. What would help you further about the existing support in CTP1? What would you need to see for Enum support in the Designer for it to be the most valuable to your development?

Feedback is always appreciated!

 

Thank you,

Sarah McDevitt

Program Manager

Comments

  • Anonymous
    August 12, 2011
    It would be really important to support enum types defined outside the designer. Most of the times we need to decorate our own enumerations (for example with localization attributes) and as things are implemented right now it's impossible. Also, it's cumbersome to modify alredy define enums because we need to open the designer for that. Cheers,

  • Anonymous
    August 12, 2011
    If I create a Enum, EF will create a table in database for it? Or is just at Model Layer? I think we dont need a table, neither inner joins to enums on queries Thx

  • Anonymous
    August 12, 2011
    EF won't create a table or look for a table for the enum. You might choose to have a table for it in the database and make all columns mapped to the enum property foreign keys, i.e. if you want the database to validate the values with referential integrity, but you don't really have to do it for enums to work.

  • Anonymous
    August 12, 2011
    Projecting to enums that are not known in the model stopped working in the June CTP. This is a breaking change. var r = (from b in blah           select (EnumThatHasNothingToDoWithTheModel)b.IntProperty           ).ToList();

  • Anonymous
    August 13, 2011
    Finally! I'm sure all the LINQ search and sort capabilities will be enabled and translated to server types.

  • Anonymous
    August 13, 2011
    @Greg: can you please send me email with more details?

  • Anonymous
    August 13, 2011
    @Shimmy: yes enum properties in LINQ queries are translated to the store columns. Enums support in the EF runtime is covered in more detail in a recent blog post.

  • Anonymous
    August 15, 2011
    I agree with Tiago, it's important to be able to use enum types specified outside of the designer, ala LinqToSql.

  • Anonymous
    August 15, 2011
    The comment has been removed

  • Anonymous
    August 19, 2011
    @Tiago and Shawn: This is very good feedback! The need to use existing enum types seems to be one of the most common requests and we are looking at addressing it. A couple of things we have discussed internally are:

  1. This is only an issue if you are using database-first or model-first. In code-first you always provide your own type
  2. We could add an annotation in the model that tells our code generation templates that the type already exists and that it should hence not be generated.
  3. We can also make mapping between enum types in .NET and in the EDM model more flexible so that you can leave your enum type empty in the model but we can still recognize your enum members in LINQ queries. It is still too early to tell whether we will be able to accomplish this though. Thanks, Diego
  • Anonymous
    August 19, 2011
    @robson: Thanks for your feedback! The way we look at this is that the general ability to perform type conversions in the mapping of scalar properties is a very useful thing that many customers want (especially when they need to work with legacy databases), but not something we really had the time to tackle in the same timeframe as enums given our other priorities. We had to keep the scope of the enum feature small to make sure we can release the minimal functionality that the majority of customers need. We initially looked for simple alternatives to enable mapping enums to strings as well as integers but then we realized it was a more complicated problem, especially if we wanted to support the majority of the functionality we will support for enums mapped to integers (e.g. flags enums, sorting, bitwise operators, etc). Until we have better solution integrated to EF my suggestion is to use a similar pattern to what in the Java world used to be the most common workaround for the lack of enum support, a class with static members:    public class Colors    {        public static readonly string Red = "Red";        public static readonly string Blue = "Blue";        public static readonly string Green = "Green";    } Our LINQ implementation will recognize access to the static members of this class, e.g. the following query:    var q =        from p in context.Products        where p.Color == Colors.Red        select p; … will translate into something like this:    SELECT    [Extent1].[Id] AS [Id],    [Extent1].[Name] AS [Name],    [Extent1].[Created] AS [Created],    [Extent1].[Color] AS [Color]    FROM [dbo].[Product] AS [Extent1]    WHERE [Extent1].[Color] = @p__linq__0 … where the parameter @p__linq__0 will take a value of ‘Red’. Hope this helps, Diego

  • Anonymous
    October 12, 2011
    It would be really helpful if a table is marked as an enum, that the EF could read that table from the database and generate the members of the enum programmatically. Having to manually add each member of the enum makes this feature more trouble than its worth in a lot of cases.

  • Anonymous
    October 13, 2011
    I hope that when you are considering linking enums from outside the model you include the ability to link enums from referenced assemblies, it would be a real shame if this was missed. It would be good if these could be I ported via the model browser and then selectable from the types drop down.

  • Anonymous
    November 15, 2011
    How close is this to being production?  I don't want to install and mess up my production setup right now, but really want this.  If we are just a month away, I will wait.

  • Anonymous
    December 01, 2011
    Nice blog.Keep posting more www.web-design-expert.com

  • Anonymous
    January 03, 2012
    I can't seem to find enum support

  • Anonymous
    January 08, 2012

  1. I see that enum support is not available in VB.NET right now. Is there any chance to get it before VS.next ships?
  2. Is the table created for an enum by code first compatible with a LightSwitch fast choice table or will it become compatible? Is see that a lightswitch fast choice table can have multiple entries with same name so long as the Id is unique.
  • Anonymous
    April 25, 2012
    Is it possible to support string as the underlying data type?

  • Anonymous
    May 29, 2012
    Hi, i've installed the package founded in the link above in this order : 1 - EF_JUNE_2011_CTP.msi 2 - DS_JUNE_2011_CTP.msi 3 - EFTools.msi but when i open a old data model or create a new one, i can't fine enum support.... any idea ?

  • Anonymous
    June 18, 2012
    I am using EF5 and love this feature. Is there a way to use the 'Description' attribute that is possible using Code-first? I am using Database-first and an edmx file and really would like to make my enums have spaces so that I can use them in Drop-down-lists.

  • Anonymous
    August 04, 2014
    Michael Johnson, Use a [Display(Name="Your Description"] on your enum and then you cna use an Html.EnumDropDownListFor control in MVC5