Imagine a class like this:
public class MyEntity : Entity
{
public virtual States States { get; set; }
}
[Flags]
public enum States
{
None,
State1 = 1,
State2 = 2,
State3 = 4,
State4 = 8
}
I want to map it to this table:
TABLE MY_ENTITY
ID: PK
STATES_ID : FK to MY_ENTITY_STATES
TABLE MY_ENTITY_STATES
ID: PK
IS_STATE1_SET (bit)
IS_STATE2_SET (bit)
IS_STATE3_SET (bit)
IS_STATE4_SET (bit)
Is this possible?
This is a legacy scenario, I can’t change the database.
As per request, here is some sample data:
Table MY_ENTITY
ID | STATES_ID
---+----------
14 | 35
Table MY_ENTITY_STATES
ID | IS_STATE1_SET | IS_STATE2_SET | IS_STATE3_SET | IS_STATE4_SET
---+---------------+---------------+---------------+--------------
35 | 0 | 1 | 0 | 1
This should lead to a value of States.State2 | States.State4 in the States property in the MyEntity instance with the ID 14.
This is a two step solution.
First, use join to bring the MY_ENTITY_STATES record, and then write a
IUserTypethat converts the flags to columns.Check here for an example.