简体   繁体   中英

Entity Framework Wrong foreign key order

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?

Not really sure what went wrong. I just tried to revert and write back all over again. Associations and keys are same as what i defined in the question, but now it works .

My Entities where or rather, are..

 [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; }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM