1

I have Customers table: Customer(id, name status_id).
I have Statuses table: Status(id, name, code).
There is relation (FK) between Customer and Status: status_id (Customer) = id (Status).

In the .NET code I have Status enum and Customer class:

enum Status {
    status_1,
    status_2,
    status_3,
    status_4
}

class Customer {
    public virtual long id {get;set;}
    public virtual string name {get;set;}
    public virtual Status customer_status {get;set;}
}

The enums doesn't have the values of the statuses rows from the db because I don't want to create dependency of the data in the code (hard coded).

How can I use the entity framework in order to present the customer class in the edmx?

EDIT:
All the solutions I found assumes that the enum values are the same as the status_id in Customer table. They even doesn't have the Status table which is very importent to me in order to make constraint on the values possibility of status_id

Naor
  • 23,465
  • 48
  • 152
  • 268

1 Answers1

4

No way. Enums are not supported at all so you must change your class to include both statusId (doesn't have to be public) and not mapped customer_status which will handle translation of database record to the enum. You must include the dependency to a recrod because your code must know which record Id represents which enum member. That is not structure mapping but data mapping which of course include data dependency.

Also do not include Statuses table to your model.

There is one approach to fake enums in EFv4 but it would require even more changing your code and enum members would probably have to use same values as database records.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • @Ladislav Mrnka: really appriciate helping me. I don't understand what do you suggest.. actually, the values the stored in the db are transparent to the code - the code shouldn't care about the values but the strings that represents the code in Status table. I thought of something like view with converting the string value to enum (same as your example link but instead of using int - using string), but I don't know how to approach it. – Naor Apr 20 '11 at 20:30
  • @Naor: That is even more complicated - you want to map value from navigation property to the enum. So you must add Statuses table to the model. You must eager load it every time you load the customer and you must again define separate non mapped property which will tranlate related entity into the enum. And be aware that non mapped properties cannot be used in linq-to-entities queries. – Ladislav Mrnka Apr 20 '11 at 20:34
  • @Ladislav Mrnka: What can I do? which "dependent values" possibility do I have?.. I have data in my tables. changing all the enums in my code is a lot of work. – Naor Apr 20 '11 at 20:45
  • @Naor: Do not change the data - you must modify your customer object. The object must be responsible for converting related "Status object" into enum. – Ladislav Mrnka Apr 20 '11 at 20:53
  • @Ladislav Mrnka: Ok, I understand. Customer will hold the status_id from the table and will convert it to the enum. But how can I do this conversion from int to enum if the enum doesn't have values? – Naor Apr 20 '11 at 21:03
  • No, that was my former idea before you mentioned that status name is important. Customer will hold reference to new `Status` object which will have id, name and code. Customer will also map the status object into the enum value. – Ladislav Mrnka Apr 20 '11 at 21:13
  • @Ladislav Mrnka: Ok, now I get it. you suggest not using enum.. Do you know if ad when ef will support enums? – Naor Apr 20 '11 at 21:21
  • Enums are highly requested feature like unique keys so I believe in the next major version. – Ladislav Mrnka Apr 20 '11 at 21:25
  • You can still use enum but you must fake it and map enum value to the object - that is just property returning enum value which will return value based on the name of related status. – Ladislav Mrnka Apr 20 '11 at 21:26
  • @Ladislav Mrnka: So I'll use StatusWrapper as POCO object to Status table and return in it's property the Status enum. – Naor Apr 20 '11 at 22:02