我一直在研究ASP.NET MVC Web应用程序,我遇到了一个无法解决的问题。我有以下课程:
public abstract class QuestionAbstract
{
public int Id { get; set; }
public int ExamId { get; set; }
public string Description { get; set; }
public virtual Exam Exam { get; set; }
public virtual ICollection<AnswerAbstract> Answers { get; set; }
public virtual ICollection<Variable> Variables { get; set; }
}
public abstract class AnswerAbstract
{
[Key, Column(Order = 0)]
public int ResponseId { get; set; }
[Key, Column(Order = 1)]
public int QuestionAbstractId { get; set; }
[ForeignKey("ResponseId")]
public virtual Response Response { get; set; }
[ForeignKey("QuestionAbstractId")]
public virtual QuestionAbstract Question { get; set; }
}
我希望数据库包含,对于AnswerAbstract,属性为ResponseId和QuestionAbstractId。但是,在创建数据库时,结果如下:
CREATE TABLE [dbo].[AnswerAbstract] (
[ResponseId] INT NOT NULL,
[QuestionId] INT NOT NULL,
[QuestionAbstract_Id] INT NULL,
CONSTRAINT [PK_dbo.AnswerAbstract] PRIMARY KEY CLUSTERED ([ResponseId] ASC, [QuestionAbstractId] ASC),
CONSTRAINT [FK_dbo.AnswerAbstract_dbo.QuestionAbstract_QuestionAbstractId] FOREIGN KEY ([QuestionId]) REFERENCES [dbo].[QuestionAbstract] ([Id]),
CONSTRAINT [FK_dbo.AnswerAbstract_dbo.Response_ResponseId] FOREIGN KEY ([ResponseId]) REFERENCES [dbo].[Response] ([Id]) ON DELETE CASCADE,
CONSTRAINT [FK_dbo.AnswerAbstract_dbo.QuestionAbstract_QuestionAbstract_Id] FOREIGN KEY ([QuestionAbstract_Id]) REFERENCES [dbo].[QuestionAbstract] ([Id])
);
如您所见,已创建了一个附加属性(QuestionAbstract_Id),其功能与我编码的QuestionId完全相同(引用QuestionAbstract类)。我怀疑这与QuestionAbstract的AnswerAbstracts集合有关,但这些类型的关系以前并没有给我带来麻烦。任何想法为什么会这样?显然我遗漏了其他课程,因为我不认为问题在另一个地方,但如果你需要任何其他代码,请随意提问。
编辑:将QuestionId更改为QuestionAbstractId。仍然是同样的问题。
答案 0 :(得分:0)
尝试更改
[ForeignKey("QuestionId")]
到
[ForeignKey("QuestionAbstractId")]
以及
public int QuestionId { get; set; }
到
public int QuestionAbstractId { get; set; }