这个问题在这里已有答案:
关于属性的MVC 4 Code First ForeignKeyAttribute ...类型...无效 1答案我首先使用实体框架代码并添加了一个类,该类具有类的列表,其中类还必须具有另一个类列表,但是当我尝试通过迁移对数据库进行更新时,我得到:
SubscaleScore类型属性“SubscaleStatistics”上的ForeignKeyAttribute无效。 在依赖类型“SubscaleScore”上找不到外键名称“SubscaleStatisticsId”。 Name值应该是以逗号分隔的外键属性名称列表。
这是我的课程的样子:
public class ExamStatistics : StatisticsData { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } [Required] public int TestId { get; set; } public IList<SubscaleStatistics> Subscales { get; set; } }public class SubscaleStatistics { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int SubscaleStatisticsId { get; set; } public int TestId { get; set; } public int SubscaleNumber { get; set; } public int ExamStatisticsId { get; set; } [ForeignKey("ExamStatisticsId")] public virtual ExamStatistics ExamStatistics { get; set; } public IList<SubscaleScore> SubscaleScores { get; set; } }
public class SubscaleScore { public int TestId { get; set; } public int Subscale { get; set; } public double Score { get; set; } public int SubscaleId { get; set; } [ForeignKey("SubscaleStatisticsId")] public virtual SubscaleStatistics SubscaleStatistics { get; set; } }
我在这做错了什么? 或者我是否需要提供更多信息才能解决问题?
This question already has an answer here:
MVC 4 Code First ForeignKeyAttribute on property … on type … is not valid 1 answerI am using entity framework code first and added a class that has a list of classes of which the classes also have to have another list of classes but when I try to do an update to the database through migrations I get this:
The ForeignKeyAttribute on property 'SubscaleStatistics' on type SubscaleScore' is not valid. The foreign key name 'SubscaleStatisticsId' was not found on the dependent type 'SubscaleScore'. The Name value should be a comma separated list of foreign key property names.
Here's what my classes look like:
public class ExamStatistics : StatisticsData { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } [Required] public int TestId { get; set; } public IList<SubscaleStatistics> Subscales { get; set; } }public class SubscaleStatistics { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int SubscaleStatisticsId { get; set; } public int TestId { get; set; } public int SubscaleNumber { get; set; } public int ExamStatisticsId { get; set; } [ForeignKey("ExamStatisticsId")] public virtual ExamStatistics ExamStatistics { get; set; } public IList<SubscaleScore> SubscaleScores { get; set; } }
public class SubscaleScore { public int TestId { get; set; } public int Subscale { get; set; } public double Score { get; set; } public int SubscaleId { get; set; } [ForeignKey("SubscaleStatisticsId")] public virtual SubscaleStatistics SubscaleStatistics { get; set; } }
What am I doing wrong here? Or do I need to provide more information to get what's wrong?
最满意答案
您需要向SubscaleScore添加外键属性。
public int SubscaleStatisticsId { get; set; }看到这里的教程: 外键
You need to add a foreign key property to SubscaleScore.
public int SubscaleStatisticsId { get; set; }see here for tutorial: foreign keys
更多推荐
发布评论