2

我們希望將我們的項目從Database-First遷移到Code-First。 對於此任務,我使用Visual Studio的數據庫生成器中的Code First。 我有一些表連接PK和FKs。 我無法將它們更改爲一個簡單的「ID」,因爲有這樣的遺留應用程序需要它們。 的EF版本6.1.3EF代碼第一個外鍵必須映射到一些AssociationSet或EntitySets參與概念方面的外鍵關聯

當我開始我PROGRAMM,我發現了以下錯誤:

(1568,10) : error 3015: Problem in mapping fragments starting at lines 1568, 1583: Foreign key constraint 'tblRechnungPosition_tblAngebReches' from table tblAngebRech (RechNr, RechPosNr, CompanyID) to table tblRechnungPosition (RechNr, CompanyID, PosNr):: Insufficient mapping: Foreign key must be mapped to some AssociationSet or EntitySets participating in a foreign key association on the conceptual side.

我想不出是什麼原因造成的錯誤,對我所有的關係似乎是正確的。

這裏是類:

public partial class tblAngebRech 
{ 
    [Key] 
    [Column(Order = 0)] 
    [DatabaseGenerated(DatabaseGeneratedOption.None)] 
    public int AngebotID { get; set; } 

    [Key] 
    [Column(Order = 1)] 
    [DatabaseGenerated(DatabaseGeneratedOption.None)] 
    public int AngebPosNr { get; set; } 

    [Key] 
    [Column(Order = 2)] 
    [DatabaseGenerated(DatabaseGeneratedOption.None)] 
    public int RechNr { get; set; } 

    [Key] 
    [Column(Order = 3)] 
    [DatabaseGenerated(DatabaseGeneratedOption.None)] 
    public int RechPosNr { get; set; } 

    [Key] 
    [Column(Order = 4)] 
    [DatabaseGenerated(DatabaseGeneratedOption.None)] 
    public int CompanyID { get; set; } 

    public DateTime Timestamp { get; set; } 

    public virtual tblAngebotPosition tblAngebotPosition { get; set; } 

    public virtual tblRechnungPosition tblRechnungPosition { get; set; } 
} 

public partial class tblRechnungPosition 
{ 
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] 
    public tblRechnungPosition() 
    { 
     tblAngebReches = new HashSet<tblAngebRech>(); 
     tblBeauftReches = new HashSet<tblBeauftRech>(); 
     tblRechPosMitarbs = new HashSet<tblRechPosMitarb>(); 
    } 

    [Key] 
    [Column(Order = 0)] 
    [DatabaseGenerated(DatabaseGeneratedOption.None)] 
    public int RechNr { get; set; } 

    [Key] 
    [Column(Order = 1)] 
    [DatabaseGenerated(DatabaseGeneratedOption.None)] 
    public int CompanyID { get; set; } 

    [Key] 
    [Column(Order = 2)] 
    [DatabaseGenerated(DatabaseGeneratedOption.None)] 
    public int PosNr { get; set; } 

    public int PosTypID { get; set; } 

    public int? StdKeyID { get; set; } 

    public double Menge { get; set; } 

    public double Betrag { get; set; } 

    [Required] 
    [StringLength(100)] 
    public string Bezeichnung { get; set; } 

    public DateTime Timestamp { get; set; } 

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
    public virtual ICollection<tblAngebRech> tblAngebReches { get; set; } 

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
    public virtual ICollection<tblBeauftRech> tblBeauftReches { get; set; } 

    public virtual tblPositionstyp tblPositionstyp { get; set; } 

    public virtual tblRechnung tblRechnung { get; set; } 

    public virtual tblStundenKey tblStundenKey { get; set; } 

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
    public virtual ICollection<tblRechPosMitarb> tblRechPosMitarbs { get; set; } 

這是protected override void OnModelCreating(DbModelBuilder modelBuilder)

modelBuilder.Entity<tblRechnungPosition>() 
    .HasMany(e => e.tblAngebReches) 
    .WithRequired(e => e.tblRechnungPosition) 
    .HasForeignKey(e => new { e.RechNr, e.CompanyID, e.RechPosNr }); 

任何幫助,將不勝感激。

回答

0

嘗試在模型構建器向手動調節FK屬性的順序:

modelBuilder.Entity<tblRechnungPosition>() 
.HasMany(e => e.tblAngebReches) 
.WithRequired(e => e.tblRechnungPosition) 
.HasForeignKey(e => new { e.RechNr, e.RechPosNr, e.CompanyID }); 

我在CodePlex上發送一個錯誤的問題向EF 6.x的團隊。 這是鏈接:https://entityframework.codeplex.com/workitem/2947

如果這不起作用。嘗試在課堂tblRechnungPosition更改列屬性的訂單號:

[Key] 
[Column(Order = 0)] 
[DatabaseGenerated(DatabaseGeneratedOption.None)] 
public int RechNr { get; set; } 

[Key] 
[Column(Order = 2)] 
[DatabaseGenerated(DatabaseGeneratedOption.None)] 
public int CompanyID { get; set; } 

[Key] 
[Column(Order = 1)] 
[DatabaseGenerated(DatabaseGeneratedOption.None)] 
public int PosNr { get; set; } 

注:你有複合外鍵。在兩個表格類別中,屬性的順序在[Columns(Order = <order number>)]內必須相同。