I have set the Keys of the entities as follows
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 entity have navigational properties to the first 3. This is defined as below.
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 });
After this when i try to read data from the table, EF is giving me the following error
(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.
When i change the order of the Foreign key definition, this error is not there. but it is unable to read the navigational property data. i checked the SQL generated in the profiler and saw that the join condition is wrong too..
say, i change to
modelBuilder.Entity<FORECAST>()
.HasRequired<SKU>(d => d.FTTSku)
.WithMany()
.HasForeignKey(k => new { k.SkuLineNum, k.DocEntry });
SQL generated is following, which is wrong too.
INNER JOIN [dbo].[SKU] AS [Extent13] ON ([Extent10].[DocEntry] = [Extent13].[Code]) AND ([Extent10].[SkuLineNum] = [Extent13].[DocEntry]) ) AS [Join7]
What could be the reason?
FORECASTandSKU, in order to reproduce the issue? - Ivan Stoev