Nullable property to entity field, Entity Framework through Code First

后端 未结 4 1636
一整个雨季
一整个雨季 2020-12-05 03:32

Using the data annotation Required like so:

[Required]
public int somefield {get; set;}

Will set somefield to Not

4条回答
  •  天涯浪人
    2020-12-05 04:22

    Jon's answer didn't work for me as I got a compiler error CS0453 C# The type must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method

    This worked for me though:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity().HasOptional(m => m.somefield);
        base.OnModelCreating(modelBuilder);
    }
    

提交回复
热议问题