【问题标题】:Entity Framework 5: Creating Collections Using Code First实体框架 5:使用代码优先创建集合
【发布时间】:2013-03-08 16:00:25
【问题描述】:

我是代码第一范式的新手,所以请多多包涵。我无法从我的实体类中获取在我的数据库上创建的基元或自定义类的集合/列表/数组。因此,我无法正确地为表播种。没有错误,只需获取一个包含除我的集合之外的所有属性的实体表。我正在使用 EF5、VS2010、MVC4 和 SqlExpress。我做错了什么?

我的实体类:

public class MyEntity
{    
    public int MyEntityID { get; set; } // GOOD
    public string Property1 { get; set; }  // GOOD
    public bool Property2 { get; set; }  // GOOD
    public IList<CustomClass> CustomClassList { get; set; }  //BAD, NOT CREATED ON DB
    public CustomClass[] CustomClassArray { get; set; }  //BAD, NOT CREATED ON DB
}

我的自定义类:

[ComplexType]
public class CustomClass
{
    public string Title { get; set; }
}

我的配置/迁移类

public class Configuration : DbMigrationsConfiguration<MyContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
        AutomaticMigrationDataLossAllowed = true;
    }

    protected override void Seed(MyContext context)
    {
        context.MyEntity.AddOrUpdate(x => x.MyEntityID,
             new MyEntity()
             {
                 Property1 = "abc",
                 Property2 = "xyz",
                 CustomClassList = new List<CustomClass> {new CustomClass{Title="Help"}}
             }
        context.SaveChanges();       
    }
}

我的 Global.asax.cs Application_Start() 方法

protected void Application_Start()
{
    WebApiConfig.Register(GlobalConfiguration.Configuration);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
    AuthConfig.RegisterAuth();
    SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Configuration>());
}

当我尝试创建一个新数据库时,我得到了相同的结果:

<!--NO LUCK-->
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS2;Initial Catalog=TESTDB_2;Integrated Security=SSPI" providerName="System.Data.SqlClient" />   
<!--NO LUCK-->
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS2;Initial Catalog=TESTDB_3;Integrated Security=SSPI" providerName="System.Data.SqlClient" />  

【问题讨论】:

  • 我无法帮助迁移,但集合应声明为 ICollection&lt;&gt;,例如public ICollection&lt;CustomClass&gt; CustomClassList { get; set; }

标签: entity-framework asp.net-mvc-4 ef-code-first entity-framework-5


【解决方案1】:

实体框架不支持原始或复杂类型的集合。如果您想要一个集合,它必须是实体的集合(即它们需要具有关键属性)。如果您有一个实体集合 - 在最简单的情况下 - 将被建模为一个单独的表,并且将根据需要创建适当的外键(取决于关系的基数)。 在您的情况下, CustomClass 被忽略,因为它不是实体并且在集合中使用(也被忽略,因为它不是实体类型的集合)。 CustomClassArray 被忽略,因为根本不支持数组,因为您无法在不重新分配数组的情况下添加/删除项目。

【讨论】:

  • 感谢您的回复。我看过 Julie Lerman 的文章,她收集了 ComplexTypes - msdn.microsoft.com/en-us/data/jj591583.aspx。也许我误解了她?
  • 这些是实体而不是复杂类型。有关更多详细信息,请参见此处msdn.microsoft.com/en-us/library/ee382831.aspx。复杂类型是(原始或复杂)属性的集合(典型示例是 Address 复杂类型) - 您可以在实体上创建复杂类型的属性以对属性进行分组,但它们将被建模为表中的列(想想:扁平化) .它们不能具有关键属性,并且您不能拥有复杂类型的集合(否则,鉴于这只是对属性进行分组的一种方式,您将如何在数据库中对其进行建模?)。
  • 很好的解释。那么为什么我的示例在复杂类型的世界中不起作用呢?看起来应该。我在示例中添加了一个键,它创建了另一个表,但 MyEntity 表中没有字段(外键?)。此外,新表没有播种。有什么想法吗?
  • 我认为这是因为你有 1-* 关系。这意味着 MyEntity 没有 CustomClass 的外键,因为可能有很多。但是,CustomClass 应该具有 CustomClass 的外键。 CustomClass 上的外键告诉他们“属于”哪个 MyEntity,您可以让我的 CustomClass 实体具有相同的外键值,这意味着它们都“属于”同一个 MyEntity 实体。
  • 这是一个 1-* 关系。所以,我需要在 CustomClass 上创建一个 MyEntityID 外键,对吧?我需要做任何映射(DbModelBuilder)吗?我想我在这之后会很好。
猜你喜欢
  • 1970-01-01
  • 2012-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-25
相关资源
最近更新 更多