2

I want to create a enum type column named 'type' but when I reverse engineer my Code First generated DB it assigns it as an 'int'.

enter image description here

Here is my Enum class:

[Flags]
public enum TypeNames
{
    Een = 0,
    Twee = 1,
    Drie = 2
}

Here is my Grounds class to create the table with the 'TypeNames'-enum. The Properties class is another table (Grounds - Properties have a TPT inheritance).

[Table("gronden")]
public partial class Grounds : Properties
{
    [Column("opp")]
    public double? Surface { get; set; }
    [EnumDataType(typeof(TypeNames)), Column("type")]
    public TypeNames Types { get; set; }
}

Any ideas of what I am missing here to get an enum-type into my DB?

4
  • 1
    And what would an enum look like in SQL ? Commented Apr 14, 2018 at 21:32
  • 1
    @HenkHolterman INT(11) hints at MySQL, in which case it would look like this. Commented Apr 14, 2018 at 21:38
  • @HenkHolterman I want to return the string-literal values instead of the index values inside my DB. But I don't know how to manage that in Code First. Commented Apr 14, 2018 at 21:48
  • 1
    There's a starting point for EF Core at this open issue on GitHub which allows columns to be created as type ENUM, but as they're returned to the client as strings, it needs more EF Core configuration to convert between the enumeration constants and strings at runtime. I do not think EF (non-Core) provides enough extension points to allow that solution to be ported though, and I get the impression it won't be possible at all. Commented Apr 14, 2018 at 21:56

1 Answer 1

1

According to the following answer, it appears that EnumDataTypeAttribute is only implemented for ASP.NET UI components, and not for EF usage. Should the EnumDataTypeAttribute work correctly in .NET 4.0 using Entity Framework?

EF Core 2.1 implements a new feature that allows Enums to be stored as strings in the database. This allows data to be self-describing, which can be really helpful for long-term maintainability. For your specific case, you could simply do:

[Table("gronden")]
public partial class Grounds : Properties
{
    [Column("opp")]
    public double? Surface { get; set; }
    [Column("type", TypeName = "nvarchar(24)")]
    public TypeNames Types { get; set; }
}

You can find more detail on this page: https://learn.microsoft.com/en-us/ef/core/modeling/value-conversions

Also, I noticed that you have the FlagsAttribute set on your enum. Are you hoping to be able to apply multiple enum values to a single entity? This should work fine when values are persisted as an int, but will not work if storing as a MySQL ENUM or string datatype. MySQL does support a SET datatype, but it seems unlikely that EF would add support for this feature, since most other databases don't have a similar concept. https://dev.mysql.com/doc/refman/5.6/en/constraint-enum.html

If you do indeed want to allow multiple enum values to be applied to each entity (similar to the way tags are used on Stack Overflow), you might consider creating a many-to-many relationship instead. Basically, this would mean converting the TypeNames enum into a Types table in the database, and allowing EF to generate a GroundTypes table to link them together. Here is a tutorial: http://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.