【问题标题】:C#, EF6 - Replace Required attribute by fluent apiC#,EF6 - 用流利的 api 替换必需的属性
【发布时间】:2017-07-29 18:00:52
【问题描述】:

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

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>>)'

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

谢谢!

【问题讨论】:

    标签: api entity-framework-6 fluent


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-28
      • 2018-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多