EF Code First外键必须映射到概念方面参与外键关联的某些AssociationSet或EntitySets

时间:2015-10-30 16:08:22

标签: c# entity-framework ef-code-first entity-framework-6 code-first

我们希望将项目从Database-First迁移到Code-First。 对于此任务,我使用Visual Studio中的数据库生成器的Code First。 我有一些连接PK和FK的表。 我无法将它们更改为一个简单的ID#,因为有一个遗留应用程序需要这种方式。 EF版本是6.1.3

当我开始编程时,我收到以下错误:

  

(1568,10):错误3015:从行开始映射片段时出现问题   1568,1583:外键约束   ' tblRechnungPosition_tblAngebReches'来自table tblAngebRech(RechNr,   RechPosNr,CompanyID)到表tblRechnungPosition(RechNr,CompanyID,   PosNr)::映射不足:外键必须映射到某些键   参与外键的AssociationSet或EntitySets   概念方面的协会。

我无法弄清楚导致错误的原因,对我而言,所有关系似乎都是正确的。

以下是课程:

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

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分: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>)]内的相同。