Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4340792
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:23:49+00:00 2026-05-21T11:23:49+00:00

I’m trying to build a generic solution to the problem of enums with EF

  • 0

I’m trying to build a generic solution to the problem of enums with EF 4.1. My solution is basically a generic version of How to fake enums in ef 4. The enum wrapper class works wonderfully in the rest of the code and allows code like:

EnumWrapper<Color> c = Color.Red;

Here’s the enum wrapper class:

public class EnumWrapper<TEnum> where TEnum : struct, IConvertible
{
    public EnumWrapper()
    {
        if (!typeof(TEnum).IsEnum)
            throw new ArgumentException("Not an enum");
    }

    public TEnum Enum { get; set; }

    public int Value
    {
        get { return Convert.ToInt32(Enum); }
        set { Enum = (TEnum)(object)value; }
    }

    public static implicit operator TEnum(EnumWrapper<TEnum> w)
    {
        if (w == null) return default(TEnum);
        else return w.Enum;
    }

    public static implicit operator EnumWrapper<TEnum>(TEnum e)
    {
        return new EnumWrapper<TEnum>() { Enum = e };
    }

    public static implicit operator int(EnumWrapper<TEnum> w)
    {
        if (w == null)
            return Convert.ToInt32(default(TEnum));
        else
            return w.Value;
    }
}

enum:

public enum Color { red = 1, green = 2, blue = 3 }

POCO:

public class ChickenSandwich 
{
    public ChickenSandwich() {
        CheeseColor = new EnumWrapper<Color>();
    }

    public int ID { get; set; }
    public string Name { get; set; }
    public EnumWrapper<Color> CheeseColor { get; set; }
}

Mapping:

public class ColorMapping : ComplexTypeConfiguration<EnumWrapper<Color>> 
{
    public ColorMapping() {
        Ignore(x => x.Enum);
        Property(x => x.Value);
    }
}

I’ve also tried mapping it on the ChickenSandwich’s EntityTypeConfiguration like this:

Property(x => x.CheeseColor.Value).HasColumnName("CheeseColor");

If I leave it up to the ColorMapping and do no explicit mapping on the ChickenSandwichMapping, it just doesn’t put it in the database. If I map it the x.CheeseColor.Value way, I get the dreaded:

System.InvalidOperationException: The
configured property ‘CheeseColor’ is
not a declared property on the entity
‘ChickenSandwich’. Verify that it has
not been explicitly excluded from the
model and that it is a valid primitive
property..


Edit

I wasn’t able to get the generic version of the enum wrapper working, so I’ve gone with writing individual wrappers. It’s not exactly what I wanted because it violates the DRY principle, but it does allow me to query the column as an enum.

[ComplexType]
public class ColorWrapper
{
    [NotMapped]
    public Color Enum { get; set; }

    public int Value
    {
        get { return (int)Enum; }
        set { Enum = (Color)value; }
    }

    public static implicit operator Color(ColorWrapper w)
    {
        if (w == null) return default(Color);

        return w.Enum;
    }

    public static implicit operator ColorWrapper(Color c)
    {
        return new ColorWrapper { Enum = c };
    }
}

I had to use the ColorWrapper on the ChickenSandwich class. It works more or less transparently. Then had to add this to my mapping class constructor to get the column name I wanted:

Property(x => x.CheeseColor.Value).HasColumnName("CheeseColorId");
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T11:23:50+00:00Added an answer on May 21, 2026 at 11:23 am

    There’s a much simpler way to map enums in EF 4: just create an int property on your ChickenSandwich class to represent the int value of the enum. That’s the property that EF should map, then have a “mini wrapper” property to allow you to use the enum

    public class ChickenSandwich 
    {   
        public int ID { get; set; }
        public string Name { get; set; }
    
        // This property will be mapped
        public int CheeseColorValue { get; set; }
    
        public Color CheseColor
        {
            get { return (Color) CheeseColorValue; }
            set { CheeseColorValue = (int) value; }
        }
    }
    

    I actually don’t have to use the Fluent API or any kind of attribute decoration for this to work. On generating the database, EF will happily ignore any type it doesn’t know how to map, but the int property will be mapped.

    I have tried mapping enums based on that article too, but it caused me no end of headaches. This method works well, and you could adapt your solution to use your wrapper as the “mapping” property, i.e. CheeseColor in this case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.