The property 'PropertyName' could not be mapped, because it is of type 'List<decimal>'
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; }
When you really need to put multiple values in single column
can use below way
Let's say you want to create only one table for below class
public class SomeClass
{
public Guid ID { get; set; }
public IEnumerable<int> Values { get; set; }
}
First create a converter
, which will control .net values to db values and vice versa
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
public class IntListToStringValueConverter : ValueConverter<IEnumerable<int>, string>
{
public IntListToStringValueConverter() : base(le => ListToString(le), (s => StringToList(s)))
{
}
public static string ListToString(IEnumerable<int> value)
{
if (value.IsEmptyCollection())
{
return null;
}
return value.Join(',');
}
public static IEnumerable<int> StringToList(string value)
{
if (value.IsNullOrEmpty())
{
return null;
}
return value.Split(',').Select(i => Convert.ToInt32(i)); ;
}
}
And DbContext
should have below method
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
.....
var IntValueConverter = new IntListToStringValueConverter();
modelBuilder
.Entity<SomeClass>()
.Property(e => e.Values)//Property
.HasConversion(IntValueConverter);
}
Done!! IT should work