entity framework - EF Code-first: Increment a non-autoincremented field manually -
i´m using existing database our erp.
in database tables, there float field called "r_e_c_n_o_", field not auto-incremented database , can´t change it.
for added entities increment field "r_e_c_n_o_", how acomplish in dbcontext´s savechanges() method?
using ado.net i´d that:
public static int getnext(string tablename, string fieldname) { var cmd = _conn.createcommand(string.format("select max({0}) + 1 {1}", fieldname, tablename)); var result = (int)cmd.executescalar(); return result; } update: please take in comment below, need solve problem:
public override int savechanges() { var entries = this.changetracker.entries(); dictionary<string, int> lastrecnos = new dictionary<string, int>(); foreach (var entry in entries) { var typename = entry.entity.gettype().name; if (lastrecnos.containskey(typename)) lastrecnos[typename]++; else lastrecnos[typename] = 0;//how can max here? int nextrecnoforthisentity = lastrecnos[typename]; var entity = entry.entity entitybase; entity.recno = nextrecnoforthisentity; } return base.savechanges(); } tks, william
Comments
Post a Comment