最初にコードを使用して SQL Server DB にテーブルと列を生成する MVC 4 アプリケーションがあります。意図していなかった追加の 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)
このテーブルは、私のスキーマでは予期されていなかったか、必要ではありませんでした。