可空属性为实体领域,实体框架通过code首先 [英] Nullable property to entity field, Entity Framework through Code First
本文介绍了可空属性为实体领域,实体框架通过code首先的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用数据注释要求
像这样:
[Required]
public int somefield {get; set;}
将设定 somefield 到不为空
数据库,如何设置的 somefield
为允许空值?我试着将它设置通过SQL Server Management Studio,但实体框架将其设置回的 不为空
的
Will set somefield to Not Null
in database, How can I set somefield
to allow NULLs?, I tried setting it through SQL Server Management Studio but Entity Framework set it back to Not Null
.
推荐答案
只是省略从字符串somefield
属性[必需]属性。这将使它创建一个数据库中的 NULL
能够列。
Just omit the [Required] attribute from the string somefield
property. This will make it create a NULL
able column in the db.
为使INT类型允许在数据库中的NULL,他们必须声明为模型中的可空整数:
To make int types allow NULLs in the database, they must be declared as nullable ints in the model:
// an int can never be null, so it will be created as NOT NULL in db
public int someintfield { get; set; }
// to have a nullable int, you need to declare it as an int?
// or as a System.Nullable<int>
public int? somenullableintfield { get; set; }
public System.Nullable<int> someothernullableintfield { get; set; }
这篇关于可空属性为实体领域,实体框架通过code首先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文