Ragnar
Ragnar

Reputation: 2690

List<Enum> could not be mapped

I have models in my .Net Core project where I want to have a List of Enum.

public class CartOptional
    {
        [Required]
        public Guid Id { get; private set; }
        public int Price { get; set; }
        public string CartId { get; set; }
        public string Type { get; set; }
        public List<FieldValidator> Validators { get; set; }
        public string Label { get; set; }
        public string Value { get; set; }
        public DateTime CreatedAt { get; set; } = DateTime.Now;
        public DateTime UpdatedAt { get; set; } = DateTime.Now;
    }

FieldValidator is indeed an Enum

EF return me this error :

The property 'CartOptional.Validators' could not be mapped, because it is of type 'List' which is not a supported primitive type or a valid entity type. Either explicitly map this property, or ignore it using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

I tried different solution like adding [Serializable]. But nothing seems to work. I don't understand why the system cannot take the underliying int

EDIT:

I tried the solution with [Flags] annotation like so

[Flags]
public enum FieldValidator
{
    REQUIRED = 1,
    ALPHABETIC = 2,
    NUMERIC = 4,
    EMAIL = 8,
    PHONE = 16,
    DATE = 32
}

And in the parent class I have public FieldValidator Validators { get; set; }

But now I try to POST a new element and I got this error when I try to validate the ModelState:

"tierList[0].fieldList[0].validators": [ "Unexpected token StartArray when parsing enum. Path 'tierList[0].fieldList[0].validators', line 24, position 25." ]

What is a token StartArray ?

Upvotes: 2

Views: 4601

Answers (2)

Kostas Dafnomilis
Kostas Dafnomilis

Reputation: 629

The problem is not enum at all. It is the List. You would get the same error even if you tried

public List<int> Validators { get; set; }

or

public List<string> Validators { get; set; }

or any other type.

This is not even a restriction of EF, but rather the database itself. You are trying to add a list of values in a table column. The databases I know don't have a configuration for such a thing.

If you want to attach a list of values in a database table, the way is to create a new table (possible a new Look Up Table in your case) that holds a foreign key to CartOptional. Something like this:

public class FieldValidatorEntity
{
    public int Id { get; set; }

    public int CartOptionalId { get; set; }
    public CartOptional CartOptional { get; set; }

    public FieldValidator Value { get; set; }
    // other columns
}

And use this property in your CartOptional entity:

public List<FieldValidatorEntity> Validators { get; set; }

A workaround would be to make Validators a string property and hold something like comma separated values, or even try a byte array if it suits you. Databases can store byte arrays in a column.

Hope I could help, merry coding!

Upvotes: 6

rickvdbosch
rickvdbosch

Reputation: 15621

One Enum value shouldn't be an issue, but since the Enum is just an integer value, you cannot use it as (a list of) entities. You should either explicitly make the Enum an entity, or use a Flags Enum instead (which I would recommend).

Have a look at Working with Enumerated Values in Entity Framework, for instance.

Upvotes: 1

Related Questions