Forum General

Dealing with DBContext lifecycle

GreekTreatGreekTreat Member ✭✭

I find it strange there isn't a good tutorial on a standard way to set up EF Core with SQLLite to be used properly in Xamarin, I've started using DI to have all my repositories instatiated but I have them currently having the same instance of DBContext. I thought this was recommended(but I may be wrong). I thought this way because we don't want to open the sqllite file everytime we need it and what if multiple thread are updating the DB at the same time. One issue that came up when trying this approach out is that if I need to update an object in the db that was already updated before I can't use _databaseContext.ProgramCode.Update(programCode); I need to pull that object with the key then do a member wise copy to all its properties. Is this the only correct approach ? is there a good sample to show this. It is very important to make sure I have a solid DB layer in the app.


  • RolandHuhnRolandHuhn USMember ✭✭

    Old question, but attaching the object to the context is maybe the way to go, instead of member wise copy.

    I am also questioning the best practices on the lifetime of a DBContext.

    What has been the best way to do it so far for you @GreekTreat ?

Sign In or Register to comment.