.net EF 4.1代码优先-将枚举包装映射为复杂类型
我正试图用EF4.1为枚举问题构建一个通用的解决方案。我的解决方案基本上是的通用版本。enum包装器类在代码的其余部分工作得非常出色,并允许以下代码:.net EF 4.1代码优先-将枚举包装映射为复杂类型,.net,entity-framework,ef-code-first,entity-framework-4.1,.net,Entity Framework,Ef Code First,Entity Framework 4.1,我正试图用EF4.1为枚举问题构建一个通用的解决方案。我的解决方案基本上是的通用版本。enum包装器类在代码的其余部分工作得非常出色,并允许以下代码: EnumWrapper<Color> c = Color.Red; POCO: 如果我把它留给ColorMapping,而不对ChickenSandwichMapping进行显式映射,它就不会把它放在数据库中。如果我将其映射为x.CheeseColor.Value方式,我会得到可怕的结果: System.InvalidOperati
EnumWrapper<Color> c = Color.Red;
POCO:
如果我把它留给ColorMapping,而不对ChickenSandwichMapping进行显式映射,它就不会把它放在数据库中。如果我将其映射为x.CheeseColor.Value方式,我会得到可怕的结果:
System.InvalidOperationException:异常
配置的属性“CheeseColor”为
不是实体上已声明的属性
“鸡三明治”。验证它是否
未被明确排除在
并且它是一个有效的原语
财产
编辑 我无法使枚举包装器的通用版本正常工作,因此我开始编写单独的包装器。这并不是我想要的,因为它违反了原则,但它确实允许我以枚举的形式查询列
[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 };
}
}
我不得不在ChickenSandwich班上使用彩色包装器。它或多或少是透明的。然后必须将其添加到我的映射类构造函数中,以获得我想要的列名:
Property(x => x.CheeseColor.Value).HasColumnName("CheeseColorId");
这可能是枚举的最佳选择,但另一个想法是只使用常量而不是枚举:
static void Main(string[] args)
{
Console.WriteLine("There are {0} red chicken sandwiches.",
sandwiches.ChickenSandwiches
.Where(s => s.Color == Color.red)
.ToArray().Length);
}
public struct Color
{
public const int red = 1;
public const int green = 2;
}
public class ChickenSandwich
{
public ChickenSandwich()
{
}
public int ID { get; set; }
public string Name { get; set; }
public int Color { get; set; }
}
public class Sandwiches : DbContext
{
public DbSet<ChickenSandwich> ChickenSandwiches { get; set; }
}
static void Main(字符串[]args)
{
控制台.WriteLine(“有{0}个红鸡三明治。”,
三明治,鸡肉三明治
.其中(s=>s.Color==Color.red)
.ToArray().Length);
}
公共结构颜色
{
公共建筑内部红色=1;
公共建筑内部绿色=2;
}
公共级切肯桑德维奇酒店
{
公共鸡三明治
{
}
公共int ID{get;set;}
公共字符串名称{get;set;}
公共int颜色{get;set;}
}
公共类三明治:DbContext
{
公共数据库集鸡三明治{get;set;}
}
在EF 4中有一种更简单的映射枚举的方法:只需在ChickenSandwich
类上创建一个int
属性来表示枚举的int值。这是EF应该映射的属性,然后有一个“迷你包装器”属性允许您使用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; }
}
}
实际上,我不必使用fluentapi或任何类型的属性修饰来工作。在生成数据库时,EF将愉快地忽略它不知道如何映射的任何类型,但是int
属性将被映射
我也尝试过基于那篇文章映射enum
,但它让我头痛不已。此方法运行良好,您可以调整解决方案,将包装器用作“映射”属性,即本例中的CheeseColor
。我通过简单地将Nathan generic enum包装器类抽象并移动,使其工作:
public static implicit operator EnumWrapper <TEnum> (TEnum e)
我没有做任何其他事情,EF4.1根据Henrik Stenbæk answer在数据库中创建了一个名为ChildSortType_Value的“ComplexType-like字段”。
工作正常
category.ChildSortType = CategorySort.AlphabeticOrder
但是将包装器与其枚举进行比较是行不通的
if(category.ChildSortType == CategorySort.AlphabeticOrder)
{
}
应将以下运算符添加到抽象类中
public static implicit operator TEnum(EnumWrapper<TEnum> w)
{
if (w == null)
return default(TEnum);
else
return w.EnumVal;
}
公共静态隐式运算符TEnum(Enumw)
{
如果(w==null)
返回默认值(十纳姆);
其他的
返回w.EnumVal;
}
这是解决此问题的一种方法,但它会破坏枚举的类型安全性。我们使用的任何传递颜色的函数现在都会传递int。但是,我们不需要对int值执行linq查询,以便它查询底层数据库吗?也就是说,我们不需要“从鸡肉三明治中的cs开始,CheeseColorValue=(int)CheeseColor.Red”@Nathan-恐怕这是正确的。在其他方法给我带来麻烦之后,我认为这是一个小小的代价。我必须说,在我的例子中,int/enum属性并不是我的where
子句的重点。我最后为每个enum使用了单独的包装器。这是重复的,但它确实允许我使用枚举进行查询,实际上我们经常使用枚举。真正的应用程序实际上不是关于鸡肉三明治。这更像是根据客户是否是公司、个人等来查询客户列表。真的很喜欢这个解决方案,希望OP(OA?OQ?)能回到它来增加一些干燥度。这个答案也是我的解决方案。比公认的答案更优雅。
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; }
}
}
public static implicit operator EnumWrapper <TEnum> (TEnum e)
public class CategorySortWrapper : EnumWrapper<CategorySort>
{
public static implicit operator CategorySortWrapper(CategorySort e)
{
return new CategorySortWrapper() { Enum = e };
}
}
public abstract 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 int(EnumWrapper<TEnum> w)
{
if (w == null)
return Convert.ToInt32(default(TEnum));
else
return w.Value;
}
}
public CategorySortWrapper ChildSortType { get; set; }
category.ChildSortType = CategorySort.AlphabeticOrder;
category.ChildSortType = CategorySort.AlphabeticOrder
if(category.ChildSortType == CategorySort.AlphabeticOrder)
{
}
public static implicit operator TEnum(EnumWrapper<TEnum> w)
{
if (w == null)
return default(TEnum);
else
return w.EnumVal;
}