EF代码第一个外键必须映射到概念侧参与外键关联的某个AssociationSet或EntitySet
本文关键字:关联 AssociationSet EntitySet 第一个 代码 映射 EF | 更新日期: 2023-09-27 18:19:52
我们希望将我们的项目从数据库优先迁移到代码优先。对于这个任务,我使用了Visual Studio中的"代码优先"数据库生成器。我有一些带有串联PK和FK的表格。我不能将它们更改为一个简单的"ID",因为有一个遗留应用程序需要这样做。EF版本为6.1.3
当我开始我的程序时,我得到以下错误:
(1568,10):错误3015:映射从行开始的片段时出现问题15681583:外键约束表tblAngebRech中的"tblRechnungPosition_tblAngebReches"(RechNr,RechPosNr,CompanyID)到表tblRechnungPosition(RechNr,PosNr)::映射不足:外键必须映射到某些参与外键的AssociationSet或EntitySet概念方面的关联。
我搞不清楚是什么原因导致了这个错误,对我来说,所有的关系似乎都是正确的。
以下是类别:
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 });
如有任何帮助,我们将不胜感激。
尝试手动将模型生成器中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中Column属性的顺序号更改为:
[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>)]
内部的顺序必须相同。