【问题标题】:Code first generating strange column代码先生成奇怪的列
【发布时间】:2013-10-09 05:23:10
【问题描述】:

我有一个 MVC 4 应用程序,它首先使用代码在我的 SQL Server DB 中生成表和列。我试图弄清楚我是如何最终得到一个不打算的额外 TABLE 的。我查看了一些问题,但没有发现我遇到的完全相同的问题。我将尝试简单地解释一下。

我添加了一个名为 Associate 的模型,用于跟踪与我的客户有业务往来的同事。每个 Associate 都需要一个 AssociateTypedID 和 RegionID 的外键。

    namespace XXX.Models
    {
        public class Associate
        {
            public int AssociateId { get; set; }
            public string AssociateName { get; set; }
            public int AddressNumber { get; set; }
            public string AddressStreet { get; set; }
            public string City { get; set; }
            public string State { get; set; }
            public string Zipcode { get; set; }
            public string MainPhoneNumber { get; set; }
            public string AssociateEmail { get; set; }
            public string AssociateWebsite { get; set; }
            public string ContactFirstName { get; set; }
            public string ContactLastName { get; set; }
            public string ContactPhoneNumber { get; set; }
            public string ContactEmail { get; set; }
            public int RegionId { get; set; }
            public int AssociateTypeId { get; set; }

            public virtual ICollection<AssociateType> AssociateTypes { get; set; }
            public virtual ICollection<Region> Regions { get; set; }
        }
    }

    namespace XXX.Models
    {
        public class AssociateType
        {
            public int AssociateTypeId { get; set; }
            public string AssociateTypeName { get; set; }

            public virtual ICollection<Associate> Associates { get; set; }
        }
    }

    namespace XXX.Models
    {
        public class Region
        {
            public int RegionId { get; set; }
            public int RegionName { get; set; }
            public int RegionDescription { get; set; }

            public virtual ICollection<Associate> Associates { get; set; }
        }
    }

    namespace XXX.Models
    {
        public class XXXDb : DbContext
        {
            public XXXDb(): base("name=DefaultConnection")
            { 

            }
            public DbSet<Associate> Associates { get; set; }
            public DbSet<AssociateType> AssociateTypes { get; set; }
            public DbSet<Region> Regions { get; set; }
        }
    }

所以我已经更新了我上面的代码,我已经非常接近我需要在我的数据库中的位置了。我生成了以下表格。

Associates、AssociateTypes 和 Regions(每个都有我期望的列)

但我现在有一个名为 RegionAssociates 的新表,其中包含以下列:

Region_RegionId (int) & Associate_AssociateId (int)

我的架构中不需要或不需要此表。

【问题讨论】:

  • 我刚刚删除了数据库中的 RegionAssociates 表,然后运行 ​​Update-Database -Force 并没有返回。

标签: sql-server-2008 asp.net-mvc-4 ef-code-first code-first entity-framework-migrations


【解决方案1】:

您的课程与您对模型的描述不符。你说的是

每个 Associate 都可以指定 AssociateType

我想同样的AssociateType可以分配给更多的Associates,所以AssociateTypeAssociate之间应该是1:N的关系。

但是 Associate 类定义了相反的关系 - 按照惯例,public virtual ICollection&lt;AssociateType&gt; AssociateType { get; set; }AssociateAssociateType 之间创建 1:N 关系。

你的类的正确定义是

public class Associate
    {
        public int AssociateId { get; set; }
        public string AssociateName { get; set; }
        public int AddressNumber { get; set; }
        public string AddressStreet { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Zipcode { get; set; }
        public string MainPhoneNumber { get; set; }
        public string AssociateEmail { get; set; }
        public string AssociateWebsite { get; set; }
        public int RegionId { get; set; }
        public int AssociateTypeId { get; set; }
        public virtual AssociateType AssociateType { get; set; }
        public string ContactFirstName { get; set; }
        public string ContactLastName { get; set; }
        public string ContactPhoneNumber { get; set; }
        public string ContactEmail { get; set; }

        public virtual ICollection<Region> Regions { get; set; }
    }

public class AssociateType
    {
        public int AssociateTypeId { get; set; }
        public string AssociateTypeName { get; set; }

        public virtual ICollection<Associate> Associates { get; set; }
    }

【讨论】:

  • 好的,我现在明白了,回顾一下我这里没有显示的另一个模型,我以正确的方式做到了,我看到了我做错了什么。只需阅读更多关于 ICollection 的内容。昨晚长夜工作,我只是不完全明白我在做什么。感谢您的帮助!
  • 好的,所以我运行了一个更新数据库迁移命令,它删除了我的 AssociateType 表。那不应该发生?
  • 不,这不应该发生。您是否已将此属性 public virtual AssociateType AssociateType { get; set; } 添加到 Associate 类?必须在外键AssociateTypeId 旁边有一个导航属性才能使其无需配置即可工作。另一种选择是使用流畅的配置。
  • 让我们假设每个 Associate 可以有一个由对应 AssociateTypeID 表示的 AssociateType 列。每个 Associate 还可以有一个由 RegionID 表示的 Region 列,我正在更新我的原始代码以显示我的 Region 类。您认为您可以更新上面的示例以显示每个类的正确设置方式吗?
  • RegionAssociates 表出现在您的数据库中,因为您正在创建 AssociateRegion 之间的 N:M 关系。如果您希望 Associate 只有一个区域,则应删除 Regions 属性并添加新的 public virtual Region Region {get; set; } 属性
【解决方案2】:

无法确定您的配置中缺少什么,因为您没有发布它,但如果您使用流利的 api,这样的事情应该可以解决问题:

modelBuilder.Entity<AssociateType>()
.HasKey(t => t.AssociateTypeId);

modelBuilder.Entity<Associate>()
.HasRequired(t => t.AssociateType)
.WithRequiredPrincipal(t => t.Associate);

以上内容改编自本文http://msdn.microsoft.com/en-us/data/jj591620.aspx

【讨论】:

  • 嗨 Olav,如果我要使用 API 来设置关系(我现在不这样做),我会将您下面的代码放在我的 Configuration.cs 文件中的迁移中文件夹对吗?
  • Fluent API 配置进入 DbContext 类的 OnModelCreating,或者最好在单独的 EntityTypeConfiguration 和 EntityTypeConfiguration 类中添加到你的 modelBuilder 中,如下所示:modelBuilder.Configurations.Add(new AssociateTypeConfiguration())。
  • 我看到了 Olav,你帮我解决了这个问题。我现在没有使用 Fluent API。但我了解如何在需要时实施它。它在我的 DbContext 类中。感谢您的帮助,但现在我正在使用 JUST POCO 课程来尝试获得我想要的结果。
猜你喜欢
  • 2014-10-29
  • 1970-01-01
  • 1970-01-01
  • 2010-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-24
相关资源
最近更新 更多