简体   繁体   English

快速linq-to-sql什么会更有效

[英]quick linq-to-sql what would be more efficient

What is the life cycle of a data context. 数据上下文的生命周期是多少?

MyDB_DataContext db = new MyDB_DataContext(); MyDB_DataContext db =新的MyDB_DataContext();

Is it more efficient to pass one context across several methods or create new instances of it. 在多个方法之间传递一个上下文或创建它的新实例是否更有效率。 OR is there any reason to choose one over the other 还是有任何理由选择一个

public void DoStuff(){
    MyDB_DataContext db = new MyDB_DataContext();
    doMoreStuff()
}
private void doMoreStuff(){
    MyDB_DataContext db = new MyDB_DataContext();
    return;
}

VS VS

public void DoStuff(){
    MyDB_DataContext db = new MyDB_DataContext();
    doMoreStuff(db)
}
private void doMoreStuff(MyDB_DataContext db){            
    return;
}

There's no hard-and-fast rules, but to give you some context, DataContexts should generally be per-request if you're writing a website. 没有严格的规则,但是为了给您一些上下文,如果您正在编写网站,则DataContexts通常应按请求进行。 Don't create it all the time, but what you definitely don't want to do is carry around the DataContext as a singleton. 不要一直创建它,但是您绝对不希望做的是将DataContext作为一个单独的对象携带。

Edit: %s/session/request/g 编辑: %s /会话/请求/ g

well in my opinion you can use one instance in all method because everytime when you call instance new method the memory get refresh and your instances get new value without effecting your recent value.well datacontext is only used to make object for dataclasses so that we can easily call any table or stored procedure in any method. 我认为您可以在所有方法中使用一个实例,因为每次调用实例新方法时,内存都会刷新,并且您的实例获得新值,而不会影响您的最新值。datacontext仅用于为数据类创建对象,这样我们就可以轻松地以任何方法调用任何表或存储过程。

As you can declare datacontext as a global variable.its one object works fine for all methods. 您可以将datacontext声明为全局变量。它的一个对象适用于所有方法。

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

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