add-migration 当实体有智能枚举时,添加迁移时报错

发布时间 2023-09-15 17:07:59作者: 宁静致远.
No suitable constructor was found for entity type 'Customer'. The following constructors had parameters that could not be bound to properties of the entity type: cannot bind 'customerLevel' in 'Customer(string name, CustomerLevel customerLevel)'.

 解决方案:

在基础设施层添加配置文件

public class CustomerConfig : IEntityTypeConfiguration<Customer>
{
  public void Configure(EntityTypeBuilder<Customer> builder)
  {
    builder.Property(p => p.CustomerLevel)
      .HasConversion(
          p => p.Value,
          p => CustomerLevel.FromValue(p));
  }
}