0

这是这个问题的后续问题,我遇到了类似的问题。但是现在默认外键约定解决了这个问题。

我现在的问题是(简而言之),我的迁移产生

int ReferencedEntityID; int ReferencedEntity_ReferencedEntityID;

其中一个是我模型中的整数属性,另一个是虚拟属性。

我的迁移产生了这个:

"dbo.Contracts",
            c => new
                {
                    ContractId = c.Int(nullable: false, identity: true),
                    PricePerUnit = c.Double(nullable: false),
                    Unit = c.Int(nullable: false),
                    Currency = c.Int(nullable: false),
                    ClientId = c.Int(nullable: false),
                    CompanyId = c.Int(nullable: false),
                    ArticleId = c.Int(nullable: false),
                    Client_ClientId = c.Int(),
                    Article_ArticleId = c.Int(),
                })

如您所见,Client & Article 被引用了两次。

这是我的模型

public class Client {
    public 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);
        }
    }

    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 Article {

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

public class Contract {

    public 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; }
  //  [ForeignKey("ClientId")]
    public virtual Client Client { get; set; }

    [Required]
    public int CompanyId { get; set; }
    //[ForeignKey("CompanyID")]
    public virtual Company Company { get; set; }

    [Required]
    public int ArticleId { get; set; }
   // [ForeignKey("ArticleId")]
    public virtual Article Article { 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().HasForeignKey(dl => dl.ArticleId).WillCascadeOnDelete(false);//.Map( dl => dl.MapKey("ArticleId"))
            modelBuilder.Entity<Contract>().HasRequired(bm => bm.Client).WithMany().HasForeignKey(dl => dl.ClientId).WillCascadeOnDelete(false);//.Map(dl => dl.MapKey("ClientId"))
            modelBuilder.Entity<Article>().HasRequired(bm => bm.Company).WithMany().HasForeignKey(dl => dl.CompanyId).WillCascadeOnDelete(false);//.Map(dl => dl.MapKey("CompanyId"))
            modelBuilder.Entity<Measurement>().HasRequired(bm => bm.Company).WithMany().HasForeignKey(dl => dl.CompanyId).WillCascadeOnDelete(false); //.Map(dl => dl.MapKey("CompanyId"))
            modelBuilder.Entity<Order>().HasRequired(bm => bm.Client).WithMany().HasForeignKey(dl => dl.ClientId).WillCascadeOnDelete(false); //.Map(dl => dl.MapKey("ClientId"))
            modelBuilder.Entity<Order>().HasRequired(bm => bm.Article).WithMany().HasForeignKey(dl => dl.ArticleId).WillCascadeOnDelete(false);//.Map(dl => dl.MapKey("ArticleId"))
            modelBuilder.Entity<IncomingMeasurement>().HasRequired(bm => bm.client).WithMany().HasForeignKey(dl => dl.ClientId).WillCascadeOnDelete(false);//.Map(dl => dl.MapKey("ClientId"))
            modelBuilder.Entity<Client>().HasOptional(c => c.MerchantReference).WithMany().HasForeignKey(dl => dl.MerchantReferenceId); //.Map(dl => dl.MapKey("MerchantReferenceId"))

            //Required fields


            base.OnModelCreating(modelBuilder);
        }

我必须做什么才能创建它们:

  • 必需的
  • 两者都在我的数据库模式中的一个属性中(应该如此)
4

1 回答 1

1

可以,甚至建议ArticleId在“真实”引用中附带原始 FK 属性(如 )。在 EF 中,这称为外键关联,而不是只有引用(如)的独立关联Article.Company

因此,您可以保持模型保持原样。您只需要指定外键。

我在您之前的问题的模型中尝试了一些类,这产生了预期的结果:

modelBuilder.Entity<Article>().HasMany(a => a.Contracts)
    .WithRequired(c => c.Article)
    .HasForeignKey(c => c.ArticleID).WillCascadeOnDelete(false);
modelBuilder.Entity<Client>().HasMany(c => c.Contracts)
    .WithRequired(c => c.Client)
    .HasForeignKey(c => c.ClientID).WillCascadeOnDelete(false);
modelBuilder.Entity<Company>().HasMany(c => c.Articles)
    .WithRequired(a => a.Company)
    .HasForeignKey(c => c.CompanyID).WillCascadeOnDelete(false);

请注意,我转过定义,因为当我按照您的方式进行操作时,HasForeignKey它仍然重复了 FK 字段。我不确定为什么。

于 2013-01-23T21:58:17.623 回答