entity framework custom data type mapping

后端 未结 2 585
傲寒
傲寒 2021-01-19 04:33

Given this code:

public class Car 
{
    public virtual int CarId { get; set; }
    public virtual string TypeName { get; set; }
    public ConvertableNullab         


        
相关标签:
2条回答
  • 2021-01-19 04:54

    "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; }
    }
    
    0 讨论(0)
  • 2021-01-19 05:02

    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.

    0 讨论(0)
提交回复
热议问题