实体框架使用代码优先生成错误的列

时间:2016-03-30 02:03:46

标签: asp.net entity-framework

我首先使用Entity Framework代码并在下面构建模型。当我因为某种原因更新数据库后在上下文中调用模型时,实体框架正在生成具有错误列名的查询。我想尝试纠正这个问题。我知道我可以使用数据注释或Fluent API,但为什么这会在列名的末尾生成1。此列不在DB或模型中。

模型

//GetTheAnswers 
var answers = context.Answers.Where(x => x.Question_QuestionId ==   selectedQuestion.QuestionId).ToList();

上下文调用

public class Question
{
    [Key]
    public int QuestionId { get; set; }
    public string QuestionText { get; set; }
    public virtual Answer CorrectAnswer { get; set; }
    public virtual List<Answer> Answers { get; set; } 
    public virtual LessonSpecification LessonSpecification { get; set; }
    public virtual GrammarStandards GrammarStandard { get; set; } 
}

堆叠错误

{“无效的列名'Question_QuestionId1'。”}

以下是供审核的问题模型

ArrayList<PersonalContact> allContacts = new ArrayList<>();

    Cursor personalContacts = getContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null,
    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);

    if (personalContacts != null && personalContacts.moveToFirst())
    {
        do
        {
            String name = personalContacts.getString(personalContacts.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String number = personalContacts.getString(personalContacts.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            //This if statement is my current hack-y fix to this.
            if(allContacts.size() < 1 || !allContacts.get(allContacts.size() - 1).getRawPhoneNumber().equals(number))
                allContacts.add(new PersonalContact(name, number));
        }
        while (personalContacts.moveToNext());
    }

    if (personalContacts != null)
        personalContacts.close();

    return allContacts;

1 个答案:

答案 0 :(得分:0)

您确定这是答案型号吗?可能还有类型问题属性的问题。您需要告诉EF(使用属性/映射)Question_QuestionId属性是[missing] Answer.Question属性的外键属性。

修改
问题。答案需要在答案表上提到问题。 EF在Answer表上创建一个名为Question_QuestionId的字段,该字段与Answer.Question_QuestionId属性冲突(也为此EF创建一个新字段)。
此新字段的名称为Question_QuestionId1。 您可以查看这个查看DDL语句(EF会在字段Question_QuestionId1上创建索引,因为关系,关系字段也可以为空)。

ExecuteNonQuery==========
CREATE TABLE [Answers] (
 [AnswerId] int not null identity(1,1)
, [AnswerText] text null
, [Question_QuestionId] int not null
, [Question_QuestionId1] int null
);
ALTER TABLE [Answers] ADD CONSTRAINT [PK_Answers_a5cb86ad] PRIMARY KEY ([AnswerId])
ExecuteNonQuery==========
CREATE TABLE [Questions] (
 [QuestionId] int not null identity(1,1)
, [QuestionText] text null
, [CorrectAnswer_AnswerId] int null
);
ALTER TABLE [Questions] ADD CONSTRAINT [PK_Questions_a5cb86ad] PRIMARY KEY ([QuestionId])
ExecuteNonQuery==========
CREATE INDEX [IX_Question_QuestionId1] ON [Answers] ([Question_QuestionId1])
ExecuteNonQuery==========
CREATE INDEX [IX_CorrectAnswer_AnswerId] ON [Questions] ([CorrectAnswer_AnswerId])

奇怪的是,使用EF 6.1.3,您的查询工作正常。

无论如何,我认为在这种情况下最好的解决方法是修复模型,即

public class Answer
{
    [Key]
    public int AnswerId { get; set; }
    public string AnswerText { get; set; }
    //public int Question_QuestionId { get; set; }
    public virtual Question Question { get; set; }
}


public class Question
{
    [Key]
    public int QuestionId { get; set; }
    public string QuestionText { get; set; }
    public virtual Answer CorrectAnswer { get; set; }
    public virtual List<Answer> Answers { get; set; }
}

class Context : DbContext
{

    public Context(DbConnection connection)
        : base(connection, false)
    {
    }

    public DbSet<Answer> Answers { get; set; }
    public DbSet<Question> Questions { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Question>()
            .HasMany(q => q.Answers)
            .WithOptional(x => x.Question);

        base.OnModelCreating(modelBuilder);

    }
}

查询将是

var answers = context.Answers.Where(x => x.Question.QuestionId == selectedQuestion.Question.QuestionId).ToList();