gpt4 book ai didi

c# - Entity Framework 错误的外键顺序

转载 作者:太空宇宙 更新时间:2023-11-03 14:51:14 25 4
gpt4 key购买 nike

我已经设置实体的键如下

    modelBuilder.Entity<SKU>().HasKey(p => new { p.DocEntry, p.Code });
modelBuilder.Entity<CUST>().HasKey(p => new { p.DocEntry, p.Code });
modelBuilder.Entity<Period>().HasKey(p => new { p.DocEntry, p.Code });
modelBuilder.Entity<FORECAST>().HasKey(p => new { p.DocEntry, p.Code });

FORECAST 实体具有前 3 个导航属性。定义如下。

 modelBuilder.Entity<FORECAST>()
.HasRequired<SKU>(d => d.FTTSku)
.WithMany()
.HasForeignKey(k => new { k.DocEntry, k.SkuLineNum });

modelBuilder.Entity<FORECAST>()
.HasRequired<CUST>(w => w.FTTCust)
.WithMany()
.HasForeignKey(k => new { k.DocEntry, k.CustLineNum });

modelBuilder.Entity<FORECAST>()
.HasRequired<Period>(w => w.Period)
.WithMany()
.HasForeignKey(k => new { k.DocEntry, k.PeriodID });

在此之后,当我尝试从表中读取数据时,EF 出现以下错误

(6,10) : error 3015: Problem in mapping fragments starting at lines 6, 56: Foreign key constraint 'FORECAST_Cust' from table FORECAST (CustLineNum, DocEntry) to table CUST (DocEntry, Code):: Insufficient mapping: Foreign key must be mapped to some AssociationSet or EntitySets participating in a foreign key association on the conceptual side.

(31,10) : error 3015: Problem in mapping fragments starting at lines 31, 56: Foreign key constraint 'FORECAST_Period' from table FORECAST (PeriodID, DocEntry) to table Period (DocEntry, Code):: Insufficient mapping: Foreign key must be mapped to some AssociationSet or EntitySets participating in a foreign key association on the conceptual side.

(41,10) : error 3015: Problem in mapping fragments starting at lines 41, 56: Foreign key constraint 'FORECAST_Sku' from table FORECAST (FTTSkuLineNum, DocEntry) to table SKU (DocEntry, Code):: Insufficient mapping: Foreign key must be mapped to some AssociationSet or EntitySets participating in a foreign key association on the conceptual side.

当我更改外键定义的顺序时,不会出现此错误。但它无法读取导航属性数据。我检查了分析器中生成的 SQL,发现连接条件也是错误的。

说,我改成

 modelBuilder.Entity<FORECAST>()
.HasRequired<SKU>(d => d.FTTSku)
.WithMany()
.HasForeignKey(k => new { k.SkuLineNum, k.DocEntry });

生成的SQL如下,也是错误的。

INNER JOIN [dbo].[SKU] AS [Extent13] ON ([Extent10].[DocEntry] = [Extent13].[Code]) AND ( [Extent10].[SkuLineNum] = [Extent13].[DocEntry]) ) AS [Join7]

可能是什么原因?

最佳答案

不太确定哪里出了问题。我只是试图恢复并重新写一遍。关联和键与我在问题中定义的相同,现在它有效

我的实体在哪里,或者更确切地说,是..

 [Table("@FORECAST")]
public class FORECAST : BindableBase
{

private int _code;
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Browsable(false)]
public int Code
{
get { return this._code; }
set { SetProperty(ref _code, value); }
}

private string _Name;
[Browsable(false)]
public string Name
{
get { return this._Name; }
set { SetProperty(ref _Name, value); }
}

private int _DocEntry;
public int DocEntry
{
get { return this._DocEntry; }
set { SetProperty(ref _DocEntry, value); }
}

private int _PeriodID;
public int PeriodID
{
get { return this._PeriodID; }
set { SetProperty(ref _PeriodID, value); }
}

private int _SkuLineNum;
public int SkuLineNum
{
get { return this._SkuLineNum; }
set { SetProperty(ref _SkuLineNum, value); }
}

private int _CustLineNum;
public int CustLineNum
{
get { return this._CustLineNum; }
set { SetProperty(ref _CustLineNum, value); }
}

private decimal _Value;
[DisplayName("Forecast value")]
public decimal Value
{
get { return this._Value; }
set { SetProperty(ref _Value, value); }
}

private CUST _FTTCust;
public virtual CUST FTTCust
{
get { return this._FTTCust; }
set { SetProperty(ref _FTTCust, value); }
}

private Period _FTTPeriod;
public virtual Period FTTPeriod
{
get { return this._FTTPeriod; }
set { SetProperty(ref _FTTPeriod, value); }
}

private SKU _FTTSku;
public virtual SKU FTTSku
{
get { return this._FTTSku; }
set { SetProperty(ref _FTTSku, value); }
}
}


[Table("@SKU")]
public partial class SKU
{

[Browsable(false)]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Code { get; set; }

[Browsable(false)]
public int DocEntry { get; set; }

[StringLength(15)]
[DisplayName("Item Code")]
public string ItemCode { get; set; }

[StringLength(100)]
[DisplayName("Item Name")]
public string Name { get; set; }

[StringLength(15)]
[DisplayName("H Level 0")]
public string Level0 { get; set; }

[StringLength(15)]
[DisplayName("H Level 1")]
public string Level1 { get; set; }

}

关于c# - Entity Framework 错误的外键顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51702068/

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