gpt4 book ai didi

c# - EF Code First 外键必须映射到一些在概念方面参与外键关联的 AssociationSet 或 EntitySet

转载 作者:行者123 更新时间:2023-11-30 18:22:50 24 4
gpt4 key购买 nike

我们想将我们的项目从数据库优先迁移到代码优先。对于此任务,我使用了 Visual Studio 数据库生成器中的代码优先。我有一些包含串联 PK 和 FK 的表格。我无法将它们更改为一个简单的“ID”,因为有一个遗留应用程序需要这种方式。EF版本为6.1.3

当我启动程序时,出现以下错误:

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

如有任何帮助,我们将不胜感激。

最佳答案

尝试在模型构建器中手动调整 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>)] 中属性的顺序必须相同.

关于c# - EF Code First 外键必须映射到一些在概念方面参与外键关联的 AssociationSet 或 EntitySet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33440310/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com