C#,EF6 - 用流畅的api替换Required属性

时间:2017-03-08 23:10:11

标签: api entity-framework-6 fluent

我想用以下流畅的api表达式替换类Notification的Gig属性上的[Required]属性。

public class NotificationConfiguration : EntityTypeConfiguration<Notification>
{
    public NotificationConfiguration()
    {
        Property(n => n.Gig).IsRequired();
    }   
}

如果我这样做,编译器会抛出错误CS0453:

The type 'Gig' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'StructuralTypeConfiguration'<Notification>.Property<T>(Expression<Func<Notification, T>>)'

我看不出原因,为什么这不起作用。

谢谢!

1 个答案:

答案 0 :(得分:0)

我的错误;-) Gig是一个具有表格表示的对象,因此它必须是该表的导航属性而不是必需字段。所以代码看起来像这样:

public class NotificationConfiguration : EntityTypeConfiguration<Notification>
{
    public NotificationConfiguration()
    {
        HasRequired(n => n.Gig);
    }
}