0

I am in the process of migrating some old legacy database/code to Microsoft Entity Framework version 6 and have come across the following problem.

The code-behind defines an enumeration and a POCO object as follows:

public enum AssetOwner {
    User = 1,
    Customer = 2,
    Supplier = 3
}

public class Asset {
   public int AssetId { get; set; }
   public string Reference { get; set; }
   public AssetOwner Owner { get; set; }
   ...
};

In itself, this is fine, but the SQL table uses, for historical reasons I assume, the enum name rather than its value to store the data.

create table [dbo].[Asset] 
(
    [AssetId]     int           not null,
    [Reference]   nvarchar(50)  not null,
    [Owner]       nvarchar(20)  not null
    ...
);

So the table might look like this:

1, 'asset-1', 'Customer'
2, 'asset-2', 'Supplier'

Is there a way (I'm using EF Fluent Configurations) to be able to map the enum's name to the database column?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Neilski
  • 4,385
  • 5
  • 41
  • 74
  • 1
    Take a look at this link: http://nodogmablog.bryanhogan.net/2014/11/saving-enums-as-strings-with-entity-framework/ – Fabio Jul 24 '15 at 13:15
  • Thanks Fabio, I looked t this and it addresses part of my problem, however it introduces other problems like making sure you only query the database using the string field versions. I think we might have to consider a more aggressive look at restructering the database. – Neilski Jul 24 '15 at 14:19

0 Answers0