Proper way to use dbcontext (Global or pass as par

2020-08-14 06:52发布

When I call a method that need dbcontext for update or insert but only want one saveChange() like following

Action: Login

        TempDBEntity context = new TempDBEntity();
        var temp = context.Users.Where(m => m.user_unique_id == 1).FirstOrDefault();
        temp.timestamp = new DateTime();
        temp.AddLog("Login");
        context.SaveChanges();

Function: AddLog

public void AddLog(string activity){
        TempDBEntity context2 = new TempDBEntity();
        var log = new UserLog();
        log.user_id = this.user_id;
        log.activity = activity;
        context2.UserLog.Add(log);
        context2.SaveChanges();
 }

As you can see, there is double SaveChanges() which I only need 1 SaveChanges().

Should I pass DBContext as another parameter for AddLog() or should I declare static variable for dbcontextin this case?

Thanks a lot.

3条回答
做自己的国王
2楼-- · 2020-08-14 07:47
I think you don't need to create new context in your AddLog function, you can pass the same first context to your AddLog function and then add your UserLogs to that without calling savechanges like this-

public void AddLog(TempDBEntity context, string activity){
        var log = new UserLog();
        log.user_id = this.user_id;
        log.activity = activity;
        context.UserLog.Add(log);
 }
查看更多
【Aperson】
3楼-- · 2020-08-14 07:48

You can use the dbcontext as follows:

Action: Login

using(TempDBEntity context = new TempDBEntity())
{
   var temp = context.Users.Where(m => m.user_unique_id == 1).FirstOrDefault();
   temp.timestamp = new DateTime();
   temp.AddLog("Login", context);
}

Function: AddLog

public void AddLog(string activity, TempDBEntity context)
{
    var log = new UserLog();
    log.user_id = this.user_id;
    log.activity = activity;
    context.UserLog.Add(log);
    context.SaveChanges(); 
}

This will properly dispose the object after its use.

查看更多
We Are One
4楼-- · 2020-08-14 07:49

In your case i would create a new dabtase context in the method you need it, because this is the easiest way and you can reuse your methods very good.

This should not make a lot of performance problems, because entity framework cache all important information over the database context, so creating a new one is very fast.

If you want optimize the amount of transactions, than i would write a kind of handler, which implements it's own SaveChanges method and hold one databse context per instance. Than you have one more abstraction layer and a nice API for later use.

Here is a simple example:

class UserLogin
{
    private TempDBEntity dbContex;

    UserLogin()
    {
        // ctor create dbContext
    }

    void Login()
    {
        // Login...
    }

    void AddLog()
    {
        // ...
    }

    void SaveChanges()
    {
        //dbContext.SaveChanges()...
    }
}

Passing a dbcontext as parameter is in my point of view not a very good solution. But this is opinion based...

查看更多
登录 后发表回答