entity framework - How to set optional field in code first approach in ASP.NET 5 and EF7 -
i working on project , set properties optional in code first approach. in class file have added [required]
properties set not null in sql , properties not have [required]
should set allow null in sql, properties contains id in it, sets not null in sql. below sample class contains properties should used when generating tables in database. set environmentlastdeployedid
property optional when creates new database in mssql.
public class version { public int id { get; set; } [required] public string packagename { get; set; } public string versionname { get; set; } public string originalpackagename { get; set; } [required] public string commitid { get; set; } public string commitmessage { get; set; } public int applicationid { get; set; } public application application { get; set; } public string createdby { get; set; } public int environmentlastdeployedid { get; set; } public environment environmentlastdeployed { get; set; } public int statusid { get; set; } public status status { get; set; } public datetime createdon { get; set; } public datetime modifiedon { get; set; } }
in context class file, tried use fluent api, isrequired
method, not isoptional
or allows me set table column allow null in database.
protected override void onmodelcreating(modelbuilder modelbuilder) { modelbuilder.entity<version>() .property(p => p.environmentlastdeployedid).isrequired(); }
if want allow null values int need use int?:
public int? applicationid { get; set; }
this map nullable column in sql, won't throw exception when trying store record null value column.
Comments
Post a Comment