代码首先生成奇怪的列

时间:2013-10-09 05:23:11

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

我有一个MVC 4应用程序,它首先使用代码在我的SQL Server DB中生成表和列。我试图弄清楚我最终是如何得到一个非预期的额外TABLE。我已经查看了一些问题,但没有找到我遇到的完全相同的问题。我将尽力解释这一点。

我添加了一个名为Associate的模型,它跟踪我的客户与之合作的员工。每个Associate都需要一个AssociateTypedID和RegionID的foriegn密钥。

    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; }
        }
    }

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

员工,AssociateTypes&amp;区域(每个区域都有我期望的列)

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

Region_RegionId(int)&amp; Associate_AssociateId(int)

我的架构中没有预期或需要此表。

2 个答案:

答案 0 :(得分:0)

无法确定您的配置中缺少什么,因为您没有发布它,但是如果您使用流畅的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

答案 1 :(得分:0)

您的课程与您对模特的描述不符。你在说

  

每位员工都可以指定AssociateType

我认为可以将同一AssociateType分配给更多Associate s,因此AssociateTypeAssociate之间应该有1:N关系。

Associate类以相反的方式定义关系 - 按惯例public virtual ICollection<AssociateType> 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; }
    }