c# - Writing to DB using EF6 in async process randomly yielding "property 'Id' is part of object's key ..." -
i have base class called servicepluginbase implements logging.
public class pluginlog { public int id { get; set; } public int? serviceid { get; set; } public string event { get; set; } public string details { get; set; } public datetime datetime { get; set; } public string user { get; set; } } public class sqlpluginlogger : ipluginlogger { //efloggincontext maps pluginlog so: // modelbuilder.entity<pluginlog>().totable("log").haskey(l => l.id) private efloggingcontext _logger = new efloggingcontext(); public iqueryable<pluginlog> logitems { { return _logger.logitems; } } public void logevent(pluginlog item) { _logger.logitems.add(item); _logger.savechanges(); } } public abstract class servicepluginbase : iplugin { private ipluginlogger _logger; public servicepluginbase(ipluginlogger logger) { _logger = logger; } protected logevent(string eventname, string details) { pluginlog _event = new pluginlog() { serviceid = this.id, event = eventname, details = details, user = thread.currentprincipal.identity.name, datetime = datetime.now }; _logger.logevent(_event); } }
now, within concrete class, log events happen. in 1 class, have asynchronous methods running -- , logging. works great. other times, i error stating "property 'id' part of object's key , cannot updated." interestingly enough, have absolutely no code updates value of id , never updates of log entries -- add new ones.
here async code 1 of plugins.
public class cptmanager : servicepluginbase { public override async task handlepluginprocessasync() { ... await processusersasync(requireduserslist, currentuserslist); } private async task processusersasync(list<extendeduser> required, list<extendeduser> current) { using (var http = new httpclient()) { var removals = currentusers.where(cu => !required.select(r => r.id).contains(cu.id)).tolist(); await disableusersasync(removals http); await addrequiredusersasync(requiredusers.where(ru => ru.mustadd).tolist()); } } private async task disableusersasync(list<extendeduser> users, httpclient client) { logevent("disable users","total disable: " + users.count()); await task.whenall(users.select(async user => { ... //http call disable user via web api string status = "disable user"; if(response.status == "error") { emailfaildisablenotification(user); status += " - fail"; } logevent(statustext, ...); if(response.status != "error") { //update foxpro database via oledbconnection (not ef) logevent("clearudf", ...); } })); } private async task addrequiredusersasync(list<extendeduser> users, httpclient client) { logevent("add required users", "total users add: " + users.count()); await task.whenall(users.select(async user => { ... //http call add user via web api logevent("add user", ...); if(response.status != "error") { //update foxpro db via oledbconnection (not ef) logevent("udf update",...); } })); } }
first, i'm confused why i'm getting error mentioned above -- "id can't updated" ... i'm not populating id field nor doing updates log file. there no related tables -- single log table.
my guess i'm doing improperly in regards asynchronous processing, i'm having trouble seeing if am.
any ideas may going on?
Comments
Post a Comment