sql server - Why can't I use database set defaults? -
why can't use database set defaults entity framework? when designed database, added appropriate default values (i.e. "default value or binding" field). seems have specify default in code anyway unless use storegeneratedpattern.computed
not allowed change value later. if don't specify default value in code, exception.
there logic here.
in entity have public int exampleint { get; set; }. let set default value of example column 100.
imagine situation entity object created. it's c# object default values being filled in runtime. exampleint initialized value 0. @ time, entity object knows nothing db. decide save entity. ef have create row , insert values specified. wait? should 0 or 100? ef have track if made change field, if create entity object, ef not aware of until attach it/save it.
just idea: if run in constructor query check default value? work moment create object filled default value. if object created ef, value overwritten have in db. actually, got better idea.
why don't use factory create new entity objects? factory can query db default value? it's still not nice, better running query in constructre. so:
public class examplefactory { public static getexample () { // want run once. example only. int defaultvalue = dbcontext.database.sqlquery("sqltogetthedefaultvalue"); return new example { exampleint = defaultvalue }; } }
so there go. know why ef can't this. , gave (bad) idea, of how can tackle this.
Comments
Post a Comment