[英]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.