LiXiang98

导航

 

问题:枚举类通过EFCore映射到数据库默认是int类型,无法在数据库中清晰的知道存储的数据是什么,需要查询文档或者查看代码。

结论:在实体类对应的的Config中增加

builder.Property(e=>e.Money.MoneyType).HasConversion<string>();

 

如:

public enum MoneyType
{
    CNY,USA
}
public record Money(MoneyType Type,double Data);
public record Product
{
      public Guid Id{get;init}
      public DateTime CreateTime{get;init;}
      public string ProductType{get;private set;}  
      public Money Price{get;private set;}
    

      public Product(){}//供EFCore使用
      public Product(string type,Money price)
    {
       this.Id = GUID.NewGUID();
       this.CreateTime = DateTime.Now;
       this.ProductType = type;
       this.Price = price;
    }
    
}

 

posted on 2022-10-08 16:45  LiXiang98  阅读(97)  评论(0)    收藏  举报