Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a foreign key column be an Enum in Entity Framework 6 code first?

I am converting EF5 DB first into EF6 code first. in the old setup there are some FKs that are bytes. and in the application are mapped to enums with the underlining type of byte. this has been working wonderfully.

Going over to code first and EF6 I found claims that enums should "just work" and indeed that seems to be the case for regular columns. I can just go from this

public byte FavPersonality {get;set;}

to this:

public Personality FavPersonality {get;set;}

but when it comes to columns that are also foreign keys I get this error :

System.ArgumentException : The ResultType of the specified expression is not
compatible with the required type. The expression ResultType is 'Edm.Byte'
but the required type is 'Model.Personality'. 

Is this something that cannot be done with EF6 + Code first?

edit:

enum is defined as :byte

like image 531
user1852503 Avatar asked Mar 26 '15 16:03

user1852503


People also ask

How do you define a foreign key in Entity Framework Code First?

To create Foreign Key, you need to use ForeignKey attribute with specifying the name of the property as parameter. You also need to specify the name of the table which is going to participate in relationship.

Can an enum be a primary key in SQL?

I would not recommend using an ENUM for a primary key type of foreign key type. Using an ENUM for a primary key means that adding a new key would involve modifying the table since the ENUM has to be modified before you can insert a new key.

Does Entity Framework support foreign keys?

When you change the relationship of the objects attached to the context by using one of the methods described above, Entity Framework needs to keep foreign keys, references, and collections in sync.

What is enum in Entity Framework?

In Entity Framework, this feature will allow you to define a property on a domain class that is an enum type and map it to a database column of an integer type. Entity Framework will then convert the database value to and from the relevant enum as it queries and saves data.


1 Answers

I just ran into this same issue with my enum being a basic number enum, but this came up as the first result in the search by message. I had a subtype on my main object where the values were of a fixed set of values. But, there were also objects for those so we could write queries against them.

public class Foo {
    [Key]
    public int Id { get; set; }

    public BarEnum BarId { get; set; }

    [ForeignKey(nameof(BarId))]
    public Bar Bar { get; set; }
}

public class Bar {
    [Key]
    public int Id { get; set; }
}

public enum BarEnum {
    Type1,
    Type2
}

This configuration gave me the same error message as described in this question:

The ResultType of the specified expression is not compatible with the required type. The expression ResultType is 'BarEnum' but the required type is 'Edm.Int'.

The resolution for this was simple: Just change the Id of Bar to use the enum as well, and everything worked without a problem. This does make sense, as there are far more values possible for an int than there are for a BarEnum.

public class Bar {
    [Key]
    public BarEnum Id { get; set; }
}
like image 165
krillgar Avatar answered Oct 27 '22 22:10

krillgar