我想知道如何使用mapper类将集合存储到数据库中。
类型'System.Collections.Generic.ICollection‘必须是非空值类型,才能将其用作泛型类型或方法System.Collections.Generic.ICollection中的参数'T’。
我的地图班
public LocatieMapper()
{
//Table
this.ToTable("Locatie");
//Properties => Columns
this.Property(p => p.Naam);
this.Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
this.Property(p => p.GemiddeldeMaandTemperaturen).IsRequired();
//Primary Key
this.HasKey(k => k.Id);
//Relationships
}
}
}这是我的本地班
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace Klimatogrammen.Models.Domain
{
public class Locatie
{
[Key]
public int Id { get; set; }
private String naam;
private ICollection<float> gemiddeldeMaandTemperaturen;//want to map this
private ICollection<float> totaleMaandNeerslag;//and this
public Locatie(String naam, ICollection<float> gemiddeldeMaandTemperaturen, ICollection<float> totaleMaandNeerslag)
{
this.Naam = naam;
// test
this.GemiddeldeMaandTemperaturen = gemiddeldeMaandTemperaturen;
this.TotaleMaandNeerslag = totaleMaandNeerslag;
}
// other code 添加构造器以获取额外信息
调用构造函数示例
temperatuur = new List<float> { 3.46F, 3.8F, 6.65F, 9.25F, 12.95F, 15.35F, 17.75F, 17.6F, 14.8F, 11.2F, 7F, 4.05F };
neerslag = new List<float> { 60.5F, 63F, 56.3F, 42.4F, 59.7F, 60F, 77.3F, 88.4F, 73.7F, 68.3F, 80.9F, 82.5F };
Locatie gent = new Locatie("Gent", temperatuur, neerslag);发布于 2015-02-18 22:48:11
您的问题是您使用的是StructuralTypeConfiguration,因此属性方法需要一个表达式来描述返回值类型的函数。ICollection<T>不是值类型(实际上,所有接口类型都是引用类型)。
尝试使用ConventionTypeConfiguration代替。
https://stackoverflow.com/questions/28595333
复制相似问题