外键属性无效?(Foreign key property not valid? (Entity framework code first) [duplicate])

编程入门 行业动态 更新时间:2024-10-28 06:22:59
外键属性无效?(Foreign key property not valid? (Entity framework code first) [duplicate])

这个问题在这里已有答案:

关于属性的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 answer

I 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

更多推荐

本文发布于:2023-07-27 15:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1292452.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   key   property   valid   Foreign

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!