简体   繁体   English

使用dbcontext的正确方法(全局或传递为参数?)

[英]Proper way to use dbcontext (Global or pass as parameter?)

When I call a method that need dbcontext for update or insert but only want one saveChange() like following 当我调用一个需要dbcontext进行updateinsert但只需要一个saveChange() ,如下所示

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 功能: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() . 如您所见,有两个SaveChanges() ,我只需要1个SaveChanges()

Should I pass DBContext as another parameter for AddLog() or should I declare static variable for dbcontext in this case? 我应该将DBContext作为AddLog()另一个参数传递,还是应该在这种情况下为dbcontext声明静态变量?

Thanks a lot. 非常感谢。

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. 在你的情况下,我会在你需要的方法中创建一个新的dabtase上下文,因为这是最简单的方法,你可以非常好地重用你的方法。

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. 如果你想优化事务量,那么我会编写一种处理程序,它实现了它自己的SaveChanges方法并为每个实例保存一个数据库上下文。 Than you have one more abstraction layer and a nice API for later use. 比你有一个更多的抽象层和一个很好的API供以后使用。

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. 在我看来,传递dbcontext作为参数不是一个很好的解决方案。 But this is opinion based... 但这是基于意见的......

You can use the dbcontext as follows: 您可以使用dbcontext,如下所示:

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 功能: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. 这将在使用后正确处理对象。

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);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM