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();