我需要在实体框架中使用代码优先设计来配置一对多关系。我知道这是一个愚蠢的约束,但它是被要求分配的。我用这些表创建了我的数据库
CREATE TABLE [dbo].[MissionQuestions] (
[MissionQuestionId] INT IDENTITY (1, 1) NOT NULL,
[missionID] INT NOT NULL ,
[userID] INT NOT NULL,
[missQuestion] VARCHAR (MAX) NOT NULL,
[missAnswer] VARCHAR (MAX) NULL,
PRIMARY KEY CLUSTERED ([MissionQuestionId] ASC),
CONSTRAINT [FK_MissionQuestions_Missions] FOREIGN KEY ([missionID]) REFERENCES [Missions]([missionID]),
CONSTRAINT [FK_MissionQuestions_Users] FOREIGN KEY ([userID]) REFERENCES [Users]([userID])
);
CREATE TABLE [dbo].[Missions] (
[missionID] INT IDENTITY (1, 1) NOT NULL,
[missionName] VARCHAR (50) NOT NULL,
[missionPresName] VARCHAR (30) NOT NULL,
[missionLanguage] VARCHAR (30) NOT NULL,
[missionClimate] VARCHAR (30) NOT NULL,
[missionReligion] VARCHAR (30) NOT NULL,
[missionImg] VARCHAR (30) NOT NULL,
PRIMARY KEY CLUSTERED ([missionID] ASC)
);
我也有一个Users表,但我不认为它在这个问题上是相关的 这是我的模型类
[Table("Missions")]
public class Missions
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int missionID { get; set; }
public String missionName { get; set; }
public String missionPresName { get; set; }
public String missionLanguage { get; set; }
public String missionClimate { get; set; }
public String missionReligion { get; set; }
public String missionImg { get; set; }
public virtual ICollection<MissionQuestions> MisisonQuestions { get; set; }
}
[Table("MissionQuestions")]
public class MissionQuestions
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int missionQuestionID { get; set; }
public String missQuestion { get; set; }
public String missAnswer { get; set; }
[ForeignKey("Missions")]
public virtual int missionID { get; set; }
public virtual Missions Missions { get; set; }
[ForeignKey("Users")]
public virtual int userID { get; set; }
public virtual Users Users { get; set; }
}
为了简要描述这些表格,应该选择“使命”,人们可以询问和回答有关每项任务的问题。因此每个任务都有0 - *“任务问题” 我以为我已经正确设置了一切,但是我收到了这个错误
MissionQuestions_Missions_Source :: Multiplicity在关系'MissionQuestions_Missions'中的角色&gt;'MissionQuestions_Missions_Source'中无效。 &gt;由于Dependent Role属性不是关键属性,因此Dependent Role的多重性的上限&gt;必须为'*'。
我猜这个错误是在设置数据库的方式,并且可能首先使用代码来设计数据库,但我正在进行一项任务,我被告知不要先使用代码。任何想法如何指定匹配的多重性?