【发布时间】: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