简体   繁体   English

为什么Entity Framework 5代码优先不映射我的枚举属性?

[英]Why is Entity Framework 5 code-first not mapping my enum property?

I have an MVC3 project using EF5 targeting ASP.NET framework 4.5 that I'm updating with some additional classes. 我有一个针对ASP.NET Framework 4.5且使用EF5MVC3项目,正在使用其他一些类进行更新。 One of my new classes has a nullable, nested enum property that is not being mapped to the database. 我的新类之一具有一个可为空的嵌套枚举属性,该属性未映射到数据库。

public class MyProblemClass
{
  public MyClass.MyEnum? MyPropertyName { get; set; }
}

Here are some of the relevant details: 以下是一些相关详细信息:

  • The code generated by running Add-Migration doesn't contain this property. 通过运行Add-Migration生成的代码不包含此属性。
  • I have created other new classes with basic enums that are mapped correctly. 我用基本的枚举创建了其他新类,它们的映射正确。
  • I have other existing classes with nested enums that are mapped correctly. 我有其他现有的具有嵌套枚举的类,它们的映射正确。
  • If I put a basic enum in the problem class it maps correctly. 如果我在问题类中放入基本枚举,它将正确映射。
  • If I put the problem enum in a different class it doesn't work there either. 如果我将问题枚举放在另一个类中,则该类也不起作用。

By basic enums I mean ones that are defined outside of a class: 基本枚举是指在类外部定义的枚举:

namespace My.Project.NewStuff
{
  public enum MyEnum
  {
    Option1,
    Option2
  }
}

By nested enums I mean ones that are defined inside of a class: 嵌套枚举是指在类内部定义的枚举:

namespace My.Project.NewStuff
{
  public static class MyClass
  {
    public enum MyEnum
    {
      Option1,
      Option2
    }
    public static string MyExtensionMethod (this MyEnum option) {...}
  }
}

The new enum and class names are actually identical to an existing working one, but in a different namespace. 实际上,新的枚举和类名称与现有的枚举和类名称相同,但是名称空间不同。 The new one has slightly different options, and different code in the extension methods. 新的选项稍有不同,扩展方法中的代码也不同。

I have tried slightly renaming the class, enum and property to see if there are any bugs related to using the same name, but this didn't help. 我尝试稍微重命名类,枚举和属性,以查看是否存在与使用相同名称相关的错误,但这无济于事。

What could be preventing my nested enum property from being mapped to the database? 是什么导致我的嵌套枚举属性无法映射到数据库?

Solution: rename the enum to something unique. 解决方案:将枚举重命名为唯一的名称。 As per this SO answer , Entity Framework doesn't support multiple classes with the same unqualified name. 按照此SO答案 ,实体框架不支持具有相同非限定名称的多个类。 This appears to include enums. 这似乎包括枚举。

Normally you would get an error similar to the following during the Add-Migration step: 通常,在“ Add-Migration步骤中,您会收到与以下类似的错误:

Schema specified is not valid. 指定的架构无效。 Errors: 错误:

The mapping of CLR type to EDM type is ambiguous because multiple CLR types match the EDM type 'MyEnum'. CLR类型到EDM类型的映射是模棱两可的,因为多个CLR类型与EDM类型“ MyEnum”匹配。

However, if the enumerators in the conflicting types are not identical, the Add-Migration doesn't show this error, and simply generates a migration that omits mapping any properties of the conflicting type. 但是,如果冲突类型中的枚举数不相同,则Add-Migration不会显示此错误,而只是生成一个迁移,从而忽略映射冲突类型的任何属性。 Making the enumerators identical in the conflicting types will reveal the error again. 使枚举数在冲突类型中相同将再次显示该错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM