c# - What is the proper way of using dbContext along with Repositories and Services -
i discovered used dbcontext badly.
i tried have single instance of dbcontext per app , injected dbservcies , next repositories.
var context = new dbentities(); simpleioc.default.register<ipaymentlogservice>(() => new paymentlogservice(context)); public paymentlogservice(installmentsentities context) { _context = context; _paymentlogrepository = new paymentlogrepository(context); }
i have repository created follows:
public paymentlogrepository(dbentities context) { _context = context; }
when adding row via entity framework this:
public int add(paymentlog entity) { _context.paymentlogs.add(entity); var affectedrows = _context.savechanges(); return numberofaddeditems; }
my _context existed whole time new pending changes done e.g via setter therefore got additional updates in database.
what pattern of using dbcontext.
should instantiated per method/action, or per service/repository?
Comments
Post a Comment