使用数据注释Required
像这样:
[Required]
public int somefield {get; set;}
将定萨姆菲尔德 to Not Null
在数据库中,我该如何设置萨姆菲尔德允许 NULL?,我尝试通过 SQL Server Management Studio 设置它,但实体框架将其设置回Not Null
.
只需省略 [Required] 属性即可string somefield
财产。这将使它创建一个NULL
数据库中的可用列。
要使 int 类型在数据库中允许 NULL,必须在模型中将它们声明为可空 int:
// 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; }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)