0

我尝试添加使用流利的API的配置如下:类型“短”必须是引用类型,以便在通用型使用它作为参数“TTargetEntity”

public class PeriodTypeMappings: EntityTypeConfiguration<PeriodType> 
    { 
     public PeriodTypeMappings() 
     { 
      this.HasKey(p => p.PeriodTypeId); 
      this.Property(p => p.PeriodTypeName).HasMaxLength(value: 25); 
      this.HasRequired(p => p.PeriodTypeName); 
      this.HasRequired(p => p.NumberOfPartitions);//compile error 
     } 
    } 

但我得到以下情况除外:

类型“短”必须是为了在通用类型或方法 “EntityTypeConfiguration.HasRequired使用它作为 参数“TTargetEntity”引用类型(例pression>)'

在最后一行this.HasRequired(p => p.NumberOfPartitions);发生异常,其中NumberOfPartitions是short类型的异常。

为什么会发生这种情况,以及如何解决这个问题,我试着说这个字段是必需的。

回答

2

已请求用于映射导航属性。你在找什么是IsRequired。但是,如果你的财产不是可以空的,它是默认需要的。你的贴图应该是这样的:

this.HasKey(p => p.PeriodTypeId); 

this.Property(p => p.PeriodTypeName) 
    .IsRequired() 
    .HasMaxLength(25); 

this.Property(p => p.NumberOfPartitions) 
    .IsRequired();