无法映射属性“PropertyName",因为它属于“List<decimal>"类型 [英] The property 'PropertyName' could not be mapped, because it is of type 'List<decimal>'
问题描述
当我尝试使用 EntityFramework Core 创建数据库时遇到了这个问题:
I got this problem when I try to create the database with EntityFramework Core:
无法映射属性Rating.RatingScores",因为它属于List"类型,它不是受支持的原始类型或有效的实体类型.显式映射此属性,或使用[NotMapped]"属性或使用OnModelCreating"中的EntityTypeBuilder.Ignore"忽略它.
The property 'Rating.RatingScores' could not be mapped, because it is of type 'List' which is not a supported primitive type or a valid entity type. Either explicitly map this property, or ignore it using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
这是课程:
public class Rating
{
public int Id { get; set; }
public List<decimal> RatingScores { get; set; }
public decimal Score
{
set => Score = value;
get => Math.Round(RatingScores.Sum() / RatingScores.Count, 1);
}
}
推荐答案
如果 Rating 类有多个 RatingScores 则是一对多关系,并且 RatingScores 属性需要自己的表,因此需要创建一个新类.
If the Rating class has multiple RatingScores you have a one-to-many relationship and the RatingScores property needs its own table, you therefore need to create a new class.
Class RatingScore
{
public int Id { get; set; }
public decimal RtSc { get; set; }
}
那么 Rating 属性将如下所示:
Then the Rating property will look like this:
public List<RatingScore> MyRatingScores { get; set; }
但是,如果每个评级都有一个 RatingScore,则您的资产不应该是一个集合.
However if each Rating has one RatingScore, your property should not be a collection.
public RatingScore MyRatingScore { get; Set; }
这篇关于无法映射属性“PropertyName",因为它属于“List<decimal>"类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!