外键 属性 无效? (Entity framework 代码优先)
Foreign key property not valid? (Entity framework code first)
我首先使用 entity framework 代码并添加了一个 class,它有一个 classes 列表,其中 classes 还必须有另一个列表class是的,但是当我尝试通过迁移对数据库进行更新时,我得到了这个:
属性 'SubscaleStatistics' 类型 SubscaleScore' 上的 ForeignKeyAttribute 无效。在依赖类型 'SubscaleScore' 上找不到外键名称 'SubscaleStatisticsId'。名称值应该是外键 属性 名称的逗号分隔列表。
这是我的 class 的样子:
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; }
}
我在这里做错了什么?还是我需要提供更多信息才能找出问题所在?
您需要向 SubscaleScore 添加外键 属性。
public int SubscaleStatisticsId { get; set; }
教程请看这里:foreign keys
外键必须是 id 而不是对象
尝试这样的事情:
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 int SubscaleStatisticsId { get; set; }
public virtual SubscaleStatistics SubscaleStatistics { get; set; }
}
用外键
对所有类做同样的事情
我首先使用 entity framework 代码并添加了一个 class,它有一个 classes 列表,其中 classes 还必须有另一个列表class是的,但是当我尝试通过迁移对数据库进行更新时,我得到了这个:
属性 'SubscaleStatistics' 类型 SubscaleScore' 上的 ForeignKeyAttribute 无效。在依赖类型 'SubscaleScore' 上找不到外键名称 'SubscaleStatisticsId'。名称值应该是外键 属性 名称的逗号分隔列表。
这是我的 class 的样子:
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; }
}
我在这里做错了什么?还是我需要提供更多信息才能找出问题所在?
您需要向 SubscaleScore 添加外键 属性。
public int SubscaleStatisticsId { get; set; }
教程请看这里:foreign keys
外键必须是 id 而不是对象
尝试这样的事情:
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 int SubscaleStatisticsId { get; set; }
public virtual SubscaleStatistics SubscaleStatistics { get; set; }
}
用外键
对所有类做同样的事情