简体   繁体   English

指定的列数必须与主键列EF的数目匹配

[英]The number of columns specified must match the number of primary key columns EF

I have two tables having composite pk's. 我有两个表具有复合pk。 The pk of TABLE1 goes into TABLE2 and they have a one to one optional relationship ie TABLE1 may have 1 TABLE2 or 0 TABLE2. TABLE1的pk进入TABLE2,它们具有一对一的可选关系,即TABLE1可以具有1 TABLE2或0 TABLE2。 I get the following exception on model creation when I insert data. 插入数据时,在创建模型时遇到以下异常。

The specified association foreign key columns 'third_table_id, fourth_table_id' are invalid. 指定的关联外键列“ third_table_id,fourth_table_id”无效。 The number of columns specified must match the number of primary key columns. 指定的列数必须与主键列数匹配。

Any help would be appreciated. 任何帮助,将不胜感激。 The pk's of the table 1 come from table 3 表1的pk来自表3

I have defined the mapping TABLE1 as: #region PROPERTIES 我已将映射TABLE1定义为:#region属性

        Property(p => p.THIRDTABLEID).HasColumnName("third_table_id").HasDatabaseGeneratedOption(DatabaseGeneratedOption.None).IsRequired();
        Property(p => p.FOURTHTABLEID).HasColumnName("fourth_table_id").HasDatabaseGeneratedOption(DatabaseGeneratedOption.None).IsRequired();

        Property(p => p.SEQID).HasColumnName("Seq_ID").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).IsRequired();

        #endregion PROEPRTIES

        #region IGNORE

        Ignore(p => p.RowState);


        #endregion IGNORE

        #region IGNORE

        ToTable("dbo.TABLE1");

        #endregion IGNORE

        #region KEYS
      //  HasKey(t => new { t.THIRDTABLEID, t.FOURTHTABLEID });
        HasKey(t => t.THIRDTABLEID);
        HasKey(t => t.FOURTHTABLEID);

        #endregion KEYS

        #region RELATIONSHIPS

        //relationship
        HasRequired(t => t.THIRDTABLEID).WithMany(c => c.TABLE1).HasForeignKey
                (t => t.THIRDTABLEID).WillCascadeOnDelete(false);

        //relationship
        HasRequired(t => t.SESN).WithMany(c => c.TABLE1).HasForeignKey
                (t => t.SESSIONID).WillCascadeOnDelete(false);

        #endregion RELATIONSHIPS

and the other TABLE2 as: 另一个TABLE2为:

        #region PROPERTIES

        Property(p => p.BPROLEDETLID).HasColumnName("bp_role_detl_id").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).IsRequired();
        Property(p => p.THIRDTABLEID).HasColumnName("third_table_id").HasDatabaseGeneratedOption(DatabaseGeneratedOption.None).IsRequired(); 
        Property(p => p.FOURTHTABLEID).HasColumnName("fourth_table_id").HasDatabaseGeneratedOption(DatabaseGeneratedOption.None).IsRequired(); ;
        Property(p => p.abc).HasColumnName("abc").IsOptional();


        #endregion PROPERTIES

        #region IGNORE

        Ignore(p => p.RowState);

        #endregion IGNORE

        #region TABLE MAPPING

        ToTable("dbo.TABLE2");

        #endregion 

        #region KEYS

      HasKey(t => new {t.THIRDTABLEID, t.FOURTHTABLEID});
        //HasKey(t => t.THIRDTABLEID);
        //HasKey(t => t.FOURTHTABLEID);

        #endregion KEYS

        #region RELATIONSHIPS

        //relationship

        HasRequired(t => t.TABLE1).WithOptional(c => c.TABLE1_DETL).Map(
            m=> m.MapKey(

                "third_table_id",
                "fourth_table_id"

                )).WillCascadeOnDelete(false);



        #endregion RELATIONSHIPS

Your first table is using HasKey twice, instead of creating a composite key. 您的第一个表两次使用HasKey,而不是创建复合键。 The second statement is just going to override the first one, so it looks like Table 1 has a single primary key, while Table 2 has a composite key of two columns. 第二条语句将覆盖第一个语句,因此表1看起来只有一个主键,而表2却有两列的组合键。 That's why it's telling you the number of columns must match. 这就是为什么它告诉您列数必须匹配的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EF 5 - 传递的主键值的数量必须与实体上定义的主键值的数量相匹配 - EF 5 - The number of primary key values passed must match the number of primary key values defined on the entity VALUES 子句中的值数必须与 INSERT 语句中指定的列数匹配 - The number of values in the VALUES clause must match the number of columns specified in the INSERT statement nhibernate外键必须具有与引用的主键相同的列数 - nhibernate Foreign key must have same number of columns as the referenced primary key 如何修复此错误:传递的主键值的数量必须与实体上定义的主键值的数量相匹配 - How to fix this error : The number of primary key values passed must match number of primary key values defined on the entity 大于值子句的插入语句必须匹配列数 - Insert statements more than values clause must match the number of columns 游标获取到in列表中声明的变量数必须与所选列的数量匹配 - cursor fetch the number of variables declared in the into list must match that of selected columns “传递的主键值的数量必须与实体上定义的主键值的数量匹配。 参数名称:keyValues”实体框架 - “The number of primary key values passed must match number of primary key values defined on the entity. Parameter name: keyValues” Entity Framework 传递的主键值的数量必须与实体上定义的键值匹配 - Number of primary key values passed must match key values defined on the entity 如何在 EF Core 中获取数据库表主键列的列表 - How to get list of database table primary key columns in EF Core 计算2个datagridview列必须具有相同的行数 - Calculating 2 datagridview columns must be the same number of rows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM