【问题标题】:Entity Framework Wrong foreign key order实体框架错误的外键顺序
【发布时间】:2019-01-13 02:02:35
【问题描述】:

我已将实体的键设置如下

    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) to table Period (DocEntry, Code):: Insufficient 映射:外键必须映射到某个 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如下,也是错误的。

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

可能是什么原因?

【问题讨论】:

  • 这确实很奇怪。您能否发布至少两个实体的相关部分,例如FORECASTSKU,以便重现该问题?
  • 是的,这很奇怪。我试图更新 .net 框架版本,但由于我的包中有一些错误,我恢复了我的解决方案并再次重新编写模型和关联。现在它似乎工作了。 :o

标签: c# sql-server entity-framework


【解决方案1】:

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

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

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

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 2015-08-01
    • 1970-01-01
    相关资源
    最近更新 更多