entity framework custom data type mapping

会有一股神秘感。 提交于 2019-12-01 19:24:48

The only solution is using something like this:

public class Car 
{
    public virtual int CarId { get; set; }
    public virtual string TypeName { get; set; }
    // This must be accessible to the mapping 
    public double? PriceData { get; set; } 

    public ConvertableNullable<double> Price 
    { 
        get { // Return data from PriceData }
        set { // Set data to PriceData }
    }
}

Your mapping will be:

modelBuilder.Entity<Car>().HasKey(x=>x.CarId);
modelBuilder.Entity<Car>().Property(x => x.TypeName);
modelBuilder.Entity<Car>().Property(x => x.PriceData).HasColumnName("Price");
modelBuilder.Entity<Car>().Ignore(x => x.Price);

The problem is that EF globally doesn't have support for custom scalar types.

"Make sure that it is a valid primitive property" - clearly the problem lies with your Nullable workaround - why not just make it a nullable double?

public class Car 
{
    public virtual int CarId { get; set; }
    public virtual string TypeName { get; set; }
    public double? Price { get; set; }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!