0

一部の外部キーは、コントラクト テーブルで二重に作成されます。(記事とクライアント)。そして会社は大丈夫です!

私のモデル:

public class Contract {
    [Key]
    public int ContractID { get; set; }
    public double PricePerUnit { get; set; }
    public int Unit { get; set; }
    public int Currency { get; set; }

    [Required]
    public int ClientID { get; set; }
    public virtual Client Client { get; set; }

    [Required]
    public int CompanyID { get; set; }
    public virtual Company Company { get; set; }

    [Required]
    public int ArticleID { get; set; }
    public virtual Article Article { get; set; }

}

public class Client {
    [Key]
    public int ClientID { get; set; }
    public string Number { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string ZipCode { get; set; }
    public string City { get; set; }
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string Memo { get; set; }
    public bool isMerchant { get; set; }

    public string Name
    {
        get
        {
            return string.Format("{0} {1}", FirstName, LastName);
        }
    }

    //[Required]
    public int? MerchantReferenceID { get; set; }
    public virtual Client MerchantReference { get; set; }
    [Required]
    public int CompanyID { get; set; }
    public virtual Company Company { get; set; }
    public virtual ICollection<Contract> Contracts { get; set; }
    public virtual ICollection<Order> Orders { get; set; }
}

public class Company
{
    [Key]
    public int CompanyID { get; set; }
    public string Name { get; set; }
    public int DeviceIncomingWeight { get; set; }
    public string ZipCode { get; set; }
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string City { get; set; }
    public bool Admin { get; set; }
    public int UnitForMeasurements { get; set; }
    public int UnitForDisplayOnDocuments { get; set; }

    public virtual ICollection<User> Users { get; set; }
    public virtual ICollection<Category> Categories { get; set; }
    public virtual ICollection<Article> Articles { get; set; }
    public virtual ICollection<Client> Clients { get; set; }
    public virtual ICollection<Location> Locations { get; set; }
    public virtual ICollection<Contract> Contracts { get; set; }
    public virtual ICollection<IncomingMeasurement> IncomingMeasurements { get; set; }
    public virtual ICollection<Measurement> Measurements { get; set; }
    public virtual ICollection<Order> Orders { get; set; }
}

public class Article {
    [Key]
    public int ArticleID { get; set; }
    [Required]
    public string Code { get; set; }
    public string Name { get; set; }
    public bool TrackStock { get; set; }
    public int CurrentStock { get; set; }
    public double? Price { get; set; }

    [Required]
    public int CompanyID { get; set; }
    public virtual Company Company { get; set; }
    [Required]
    public int CategoryID { get; set; }
    public virtual Category Category { get; set; }

    public virtual ICollection<Contract> Contracts { get; set; }
    public virtual ICollection<Order> Orders { get; set; }
}

これは私の OnModelCreating です。おそらく問題は次のとおりです。

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
       // modelBuilder.Entity<Contract>().HasRequired(bm => bm.Company).WithMany().WillCascadeOnDelete(false);
        modelBuilder.Entity<Contract>().HasRequired(bm => bm.Article).WithMany().WillCascadeOnDelete(false);
        modelBuilder.Entity<Contract>().HasRequired(bm => bm.Client ).WithMany().WillCascadeOnDelete(false);
        modelBuilder.Entity<Article>().HasRequired(bm => bm.Company).WithMany().WillCascadeOnDelete(false);
        modelBuilder.Entity<Measurement>().HasRequired(bm => bm.Company).WithMany().WillCascadeOnDelete(false);
        modelBuilder.Entity<Order>().HasRequired(bm => bm.Client).WithMany().WillCascadeOnDelete(false);
        modelBuilder.Entity<Order>().HasRequired(bm => bm.Article).WithMany().WillCascadeOnDelete(false);
        modelBuilder.Entity<IncomingMeasurement>().HasRequired(bm => bm.client).WithMany().WillCascadeOnDelete(false);
        modelBuilder.Entity<Client>().HasOptional(c => c.MerchantReference).WithMany().HasForeignKey(c => c.MerchantReferenceID);

        //Required fields


        base.OnModelCreating(modelBuilder);
    }

そして、私のデータベース(私のSQLサーバー)で奇妙なことが起こっています。つまり、これは私の作成テーブルスキーマです。

これらは私のフィールドです:

CREATE TABLE [dbo].[Contracts](
[ContractID] [int] IDENTITY(1,1) NOT NULL,
[PricePerUnit] [float] NOT NULL,
[Unit] [int] NOT NULL,
[Currency] [int] NOT NULL,
[ClientID] [int] NOT NULL,
[CompanyID] [int] NOT NULL,
[ArticleID] [int] NOT NULL,
[Client_ClientID] [int] NOT NULL,
[Article_ArticleID] [int] NOT NULL,
[Client_ClientID1] [int] NULL,
[Article_ArticleID1] [int] NULL,

気がつけば、[Client_ClientID] が重複しています: [Client_ClientID1] と [Article_ArticleID1] の [Article_ArticleID] です。しかし、会社はそうではありません。

これを修正する方法について何か考えはありますか?

4

2 に答える 2

1

これは、エンティティ クラスに冗長な (外部キー) 列が含まれているために発生します。たとえば、Contractクラスのカテゴリを見てください。

public class Contract
{
    public Int32 CategoryID { get; set; }

    public virtual Category Category { get; set; }
}

プロパティと列を手動で指定すると、Entity Frameworkは、プロパティによって参照されるCategoryID外部キーを保持する別の列を生成します。CategoryCategory

したがって、参照されるカテゴリの ID が必要な場合は、プロパティCategoryIDを削除して代わりに使用してください。contract.Category.CategoryID

アップデート

外部キープロパティを含めるという提案については知りませんでしたが、Jeff Siever の回答へのコメントにリンクされている記事を見て、おそらく セクションでの回答を見つけました。

Entity Framework は規則を使用して、ナビゲーション プロパティと外部キー プロパティの名前を一致させます。既定の規則は、大文字と一緒に使用するNavigationPropertyNameIdorです。NavigationPropertyName_IdNavigationPropertyNameIDD

したがって、いくつかのオプションがあります-名前を使用するように変更するか、規則をId置き換えるか、規則をオーバーライドします。

于 2013-01-23T20:29:47.013 に答える
0

モデルから重複した情報を削除します。参照されたオブジェクトの ID は必要なく、問題の原因となっています。

public class Contract {
    [Key]
    public int ContractID { get; set; }
    public double PricePerUnit { get; set; }
    public int Unit { get; set; }
    public int Currency { get; set; }

    public virtual Client Client { get; set; }

    public virtual Company Company { get; set; }

    public virtual Article Article { get; set; }
}

ID の Required 属性の代わりに、子が必須になるようにエンティティを設定する必要があります。

于 2013-01-23T20:25:58.250 に答える