我按如下所示设置了实体的键
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):错误3015:映射从第6行开始的片段时出现问题 56:来自表FORECAST的外键约束'FORECAST_Cust' (CustLineNum,DocEntry)到表CUST(DocEntry,Code)::不足 映射:外键必须映射到某些AssociationSet或 实体集参与外键关联 概念方面。
(31,10):错误3015:映射从行开始的片段时出现问题 31、56:表FORECAST中的外键约束'FORECAST_Period' (PeriodID,DocEntry)到表周期(DocEntry,代码)::不足 映射:外键必须映射到某些AssociationSet或 实体集参与外键关联 概念方面。
(41,10):错误3015:映射从行开始的片段时出现问题 41、56:表FORECAST中的外键约束'FORECAST_Sku' (FTTSkuLineNum,DocEntry)到表SKU(DocEntry,Code)::不足 映射:外键必须映射到某些AssociationSet或 实体集参与外键关联 概念方面。
当我更改外键定义的顺序时,此错误不存在。但它无法读取导航属性数据。我检查了探查器中生成的SQL,并发现联接条件也是错误的。
例如,我更改为
modelBuilder.Entity<FORECAST>()
.HasRequired<SKU>(d => d.FTTSku)
.WithMany()
.HasForeignKey(k => new { k.SkuLineNum, k.DocEntry });
正在生成SQL,这也是错误的。
内部联接[dbo]。[SKU]沿[Extent13]开启([Extent10]。 [DocEntry] = [Extent13]。 [代码] )和( [Extent10]。 [SkuLineNum] = [Extent13]。 [DocEntry] ))AS [Join7]
可能是什么原因?
答案 0 :(得分:0)
不太确定出了什么问题。我只是尝试还原并重新写回所有内容。关联和键与我在问题中定义的相同,但现在它 有效 。
“我的实体”所在的位置。
[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; }
}