0

最初にコードを使用して 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)

このテーブルは、私のスキーマでは予期されていなかったか、必要ではありませんでした。

4

2 に答える 2

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から改作されています

于 2013-10-09T05:51:57.123 に答える
0

クラスがモデルの説明と一致しません。あなたが言っています

各 Associate は AssociateType の指定を持つことができます

同じAssociateTypeものをより多くの に割り当てることができると思いますので、 と の間にAssociateは 1:N の関係があるはずです。AssociateTypeAssociate

しかし、Associateクラスはその関係を逆に定義します。慣例により、 と の間にpublic virtual ICollection<AssociateType> AssociateType { get; set; }1:N の関係が作成されます。AssociateAssociateType

あなたのクラスの正しい定義は

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; }
    }
于 2013-10-09T09:29:57.497 に答える