What can't I change enum type to enum list type?

saurav.rox

New member
Joined
Apr 13, 2023
Messages
3
Programming Experience
1-3
I have been using an enum to save a field value in the database. The enum type stores the values in the integer form. However, now I want the field type to be changed to list enum. But when I apply update-database, it throws an error: column "ProgramCredit" cannot be cast automatically to type integer[].

Previously, ProgramCredit field was just an enum type declared as: public AccountProgramCreditEnum ProgramCredit { get; set; }. This generated the column type as integer. As shown in the screenshot below:

1681369600476.png


Now I want to change it as: public List<AccountProgramCreditEnum>? ProgramCredit { get; set; } so that this column would change to List as integer[].

The database I am using is Postgres.

Any help would be more than appreciable.
 
You can't store a list in a single column like that. Each column can only contain one value per row. If you want multiple values against a single row then you need an additional table with a 1:many relation.

The alternative would be to define your enum such that you can use composite values. This is done by defining each value as a power of 2, so each bit in the binary representation corresponds to one value. For instance, if you have this type:
C#:
[Flags]
public enum Test
{
    First = 1,
    Second = 2,
    Third = 4,
    Fourth = 8
}
then you can save the value 10 to represent both Second and Fourth.
 
@jmcilhinney Thanks for replying.
I have another field defined as a list. Check the screenshot of the database below:

1681376232593.png


Here is how it is defined in the code: public List<ProjectValidatorVerifierStandardEnum>? ProjectValidatorVerifierStandard { get; set; }
 
That may be using enum support in EF itself. I've never actually done that so there are presumably relevant points I don't understand. Are both enums declared the same way?
 
Show us all meaningful differences between these two things you claim are functionally identical, yet are treated differently
 

Latest posts

Back
Top Bottom