无法映射属性“ PropertyName”,因为它的类型为“ List< decimal>” [英] The property 'PropertyName' could not be mapped, because it is of type 'List<decimal>'

查看:336
本文介绍了无法映射属性“ PropertyName”,因为它的类型为“ List< decimal>”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用EntityFramework Core创建数据库时遇到了这个问题:

I got this problem when I try to create the database with EntityFramework Core:


属性'Rating.RatingScores'不能为已映射,因为它的类型为列表,而不是受支持的原始类型或有效实体类型。要么显式映射此属性,要么使用'[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; }

但是,如果每个Rating具有一个RatingScore,则您的财产不应是集合。

However if each Rating has one RatingScore, your property should not be a collection.

public RatingScore MyRatingScore { get; Set; }

这篇关于无法映射属性“ PropertyName”,因为它的类型为“ List&lt; decimal&gt;”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆