EF 6.1 Code First - 指定的列数必须与主键列数匹配 [英] EF 6.1 Code First - the number of columns specified must match the number of primary key columns

查看:116
本文介绍了EF 6.1 Code First - 指定的列数必须与主键列数匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



指定的关联外键列'question_set_id'无效。指定的列数必须与主键列数匹配。



我删除了原始的主键QuestionSetId并创建了一个复合键关系。复合键关系中的列也映射到外键。我不知道问题是什么。



这是关联的实体。

 code> public class QuestionSet 
{
[Key,Column(Order = 1)]
public long TitleId {get;组; }

[ForeignKey(TitleId)]
public virtual标题标题{get;组; }

[Key,Column(Order = 0)]
public long ReviewCycleId {get;组; }

[ForeignKey(ReviewCycleId)]
public virtual ReviewCycle ReviewCycle {get;组; }

public virtual List< Question>问题{get;组;
}

DbContext有:

  modelBuilder.Entity< QuestionSet>()
.HasMany(c => c.Questions)
.WithMany(c => c.QuestionSets )
.Map(x => x.ToTable(QUESTION_SET_QUESTION)
.MapLeftKey(question_set_id)
.MapRightKey(question_id)
;


解决方案

QuestionSet 有两个键,但 MapLeftKey QuestionSet 只能说明一个键。

  MapLeftKey(question_set_id)

将其替换为:

  MapLeftKey(new [] {question_set_review_cycle_id,question_set_title_id})

第一个键是 ReviewCycleId (列的顺序0 ),第二个键是 TitleId (列的顺序1)。



它应该解决问题,除非问题也有两个键。


I am running into the following error while trying to migrate a database in Entity Framework.

The specified association foreign key columns 'question_set_id' are invalid. The number of columns specified must match the number of primary key columns.

I dropped the original primary key QuestionSetId and created a composite key relationship. The columns in the composite key relationship also map to foreign keys. I'm not sure what the issue is.

Here is the associated entity.

public class QuestionSet
{
    [Key, Column(Order = 1)]
    public long TitleId { get; set; }

    [ForeignKey("TitleId")]
    public virtual Title Title { get; set; }

    [Key, Column(Order = 0)]
    public long ReviewCycleId { get; set; }

    [ForeignKey("ReviewCycleId")]
    public virtual ReviewCycle ReviewCycle { get; set; }

    public virtual List<Question> Questions { get; set; }
}

The DbContext has:

    modelBuilder.Entity<QuestionSet>()
            .HasMany(c => c.Questions)
            .WithMany(c => c.QuestionSets)
                .Map(x => x.ToTable("QUESTION_SET_QUESTION")
                    .MapLeftKey("question_set_id")
                    .MapRightKey("question_id"))
        ;

解决方案

The QuestionSet has two keys, but the MapLeftKey of QuestionSet only spefified one key.

MapLeftKey("question_set_id")

Replace it with something like this:

MapLeftKey(new []{ "question_set_review_cycle_id", "question_set_title_id" })

The first key is ReviewCycleId (Column's Order 0), the second key is TitleId (Column's Order 1).

It should fix the problem, unless the Question also has two keys.

这篇关于EF 6.1 Code First - 指定的列数必须与主键列数匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆