Warm tip: This article is reproduced from serverfault.com, please click

The property 'PropertyName' could not be mapped, because it is of type 'List'

发布于 2018-08-02 17:32:44

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

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'.

Here is the class:

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);
    }
}
Questioner
Eduard Stefanescu
Viewed
0
305 2019-08-20 18:35:46

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; }
}

Then the Rating property will look like this:

public List<RatingScore> MyRatingScores { get; set; }

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

public RatingScore MyRatingScore { get; Set; }