C# 类型';短';必须是引用类型才能将其用作参数';t目标实体';在泛型类型中
我尝试使用Fluent API添加配置,如下所示:C# 类型';短';必须是引用类型才能将其用作参数';t目标实体';在泛型类型中,c#,asp.net,entity-framework,ef-code-first,entity-framework-6,C#,Asp.net,Entity Framework,Ef Code First,Entity Framework 6,我尝试使用Fluent API添加配置,如下所示: public class PeriodTypeMappings: EntityTypeConfiguration<PeriodType> { public PeriodTypeMappings() { this.HasKey(p => p.PeriodTypeId); this.Property(p => p.PeriodTypeNa
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
}
}
公共类PeriodTypeMappings:EntityTypeConfiguration
{
公共PeriodTypeMappings()
{
this.HasKey(p=>p.PeriodTypeId);
this.Property(p=>p.PeriodTypeName).HasMaxLength(值:25);
this.HasRequired(p=>p.PeriodTypeName);
this.HasRequired(p=>p.NumberOfPartitions);//编译错误
}
}
但我有以下例外: 类型“short”必须是引用类型才能用作 泛型类型或方法中的参数“TTargetEntity” 'EntityTypeConfiguration.HasRequired(表达式>)' 异常发生在最后一行
this.HasRequired(p=>p.NumberOfPartitions)代码>其中NumberOfPartitions
为short类型
为什么会发生这种情况以及如何解决这个问题,我试图说这个字段是必需的。HasRequired用于映射导航属性。你要找的东西是必需的。但如果您的属性不可为null,则默认情况下它是必需的。您的映射应如下所示:
this.HasKey(p => p.PeriodTypeId);
this.Property(p => p.PeriodTypeName)
.IsRequired()
.HasMaxLength(25);
this.Property(p => p.NumberOfPartitions)
.IsRequired();
HasRequired用于映射导航属性。你要找的东西是必需的。但如果您的属性不可为null,则默认情况下它是必需的。您的映射应如下所示:
this.HasKey(p => p.PeriodTypeId);
this.Property(p => p.PeriodTypeName)
.IsRequired()
.HasMaxLength(25);
this.Property(p => p.NumberOfPartitions)
.IsRequired();