c# - How to create a default constraint in EF code-first and Azure? -
i have following code-first entity framework code i'm using azure mobile services:
public class bookcontext : dbcontext { private const string connectionstringname = "name=ms_tableconnectionstring"; public bookcontext() : base(connectionstringname) { } // collections queried directly. public dbset<book> books{ get; set; } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { // standard code add "service" columns such createdat, updatedat, , deleted. modelbuilder.conventions.add( new attributetocolumnannotationconvention<tablecolumnattribute, string>( "servicetablecolumn", (property, attributes) => attributes.single().columntype.tostring())); // add default value 0 on deleted column. following code not work. modelbuilder.entity<book>() .property(p => p.deleted) .hasdatabasegeneratedoption(databasegeneratedoption.computed); } } as noted above, code supposed add default constraint on deleted column, not anything. i'd achieve equivalent of following sql statement ef code-first:
alter table dbo.books add constraint df_booksdeleted default 0 deleted apparently, must doing wrong. point me in right direction?
afaik, default values database part of ef core, more details, refer default values.
based on requirement, trying set default value system column deleted of azure mobile service tables. assumed override entitytablesqlgenerator.cs , add logic setting default value system column deleted , custom columns follows:
myentitytablesqlgenerator.cs
public class myentitytablesqlgenerator : entitytablesqlgenerator { protected override void updatetablecolumn(columnmodel columnmodel, tablecolumntype tablecolumntype) { //for system column deleted switch (tablecolumntype) { case tablecolumntype.deleted: columnmodel.defaultvalue = 0; break; } base.updatetablecolumn(columnmodel, tablecolumntype); } protected override void generate(createtableoperation createtableoperation) { //for custom columns foreach (columnmodel column in createtableoperation.columns) { tablecolumntype tablecolumntype = this.gettablecolumntype(column); if (tablecolumntype == tablecolumntype.none) { if (column.annotations.keys.contains("defaultvalue")) { var value = convert.changetype(column.annotations["defaultvalue"].newvalue, column.clrdefaultvalue.gettype()); column.defaultvalue = value; } } } //for system columns base.generate(createtableoperation); } } modify configuration.cs , add custom sqlgenerator:
public configuration() { setsqlgenerator("system.data.sqlclient", new myentitytablesqlgenerator()); } then, modify onmodelcreating method follows:
protected override void onmodelcreating(dbmodelbuilder modelbuilder) { //set default value custom columns modelbuilder.entity<tag>() .property(p => p.status) .hascolumnname("status") .hascolumnannotation("defaultvalue","false"); modelbuilder.conventions.add( new attributetocolumnannotationconvention<tablecolumnattribute, string>( "servicetablecolumn", (property, attributes) => attributes.single().columntype.tostring())); } result:

Comments
Post a Comment