我已将实体的密钥设置如下
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 });
预测实体具有前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_Cust‘从表预测(CustLineNum,DocEntry)到表CUST (DocEntry,Code):映射不足:必须将外键映射到概念侧参与外键关联的某个AssociationSet或EntitySets。 ( 31,10):错误3015:从第31、56行开始的片段映射问题:外键约束'FORECAST_Period‘从表预测(PeriodID,DocEntry)到表周期(DocEntry,Code):映射不足:必须将外键映射到概念侧参与外键关联的某个AssociationSet或EntitySets。 ( 41,10):错误3015:从第41、56行开始的片段映射问题:外键约束'FORECAST_Sku‘从表预测(FTTSkuLineNum,DocEntry)到表SKU (DocEntry,Code):映射不足:必须将外键映射到概念侧参与外键关联的某个AssociationSet或EntitySets。
当我更改外键定义的顺序时,不会出现此错误。但是它无法读取导航属性数据。我检查了分析器中生成的SQL,发现联接条件也是错误的。
说,我换到
modelBuilder.Entity<FORECAST>()
.HasRequired<SKU>(d => d.FTTSku)
.WithMany()
.HasForeignKey(k => new { k.SkuLineNum, k.DocEntry });
生成的SQL如下所示,这也是错误的。
在(Extent10.DocEntry = Extent13.Code)上以Extent13形式加入dbo.SKU,以Join7形式加入(Extent10.SkuLineNum = Extent13.DocEntry) )
可能是什么原因?
发布于 2018-08-07 06:22:33
不太确定到底出了什么问题。我只是试着回复,然后再写一遍。关联和键与我在问题中定义的相同,,但现在它--工作。
我的实体,或者更确切地说,在哪里..。
[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; }
}
https://stackoverflow.com/questions/51702068
复制相似问题