Mapping with EF Core enumeration property

.net c# entity-framework entity-framework-core

Question

I have class Person which has property CardType

public class Person{
    protected Person(){}    
    public Person(CardType cardType){
       cardType = CardType;
    }

    public CardType CardType { get; private set; }
    ... other properties ommited
}



public class CardType : Enumeration
{
    public static CardType Amex = new CardType(1, "Amex");
    public static CardType Visa = new CardType(2, "Visa");
    public static CardType MasterCard = new CardType(3, "MasterCard");

    public CardType(int id, string name)
        : base(id, name)
    {
    }
}

I'm trying to map CardType property using EF Core

public void Configure(EntityTypeBuilder<Person> builder)
{
   builder.ToTable("Person");
   builder.Property(x => x.CardType);
}

But I'm getting following:

The property 'Person.CardType' is of type 'CardType' which is not supported by current database provider. Either change the property CLR type or ignore the property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

1
0
9/16/2019 9:06:13 PM

Popular Answer

You cannot use custom classes as types for Data properties (they can be used in navigation properties only). You need to use enum:

public enum CardType
{
    Default = 0,
    Amex,
    Visa,
    MasterCard
}
0
9/18/2019 7:44:42 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow