Nested DbContext due to method calls - Entity Framework

后端 未结 4 1715
广开言路
广开言路 2020-12-13 04:21

In the following case where two DbContexts are nested due to method calls:

public void Method_A() {
    using (var db = new SomeDbContext()) {
        //...d         


        
4条回答
  •  一整个雨季
    2020-12-13 04:38

    If you think number of connections to Database,and impact of times that new connections must be opened, not an important problem and you have no limitation for support your application to run at best performance, everything is OK. Your code works well. Because create just a db context has a low impact in your performance,meta data will be cached after first loading, and connection to your database just occurs when the code need to execute a query. With liitle performance consideration and code design, I offer you to make context factory to have just an instance of each Db Context for each instance of your application.

    You can take a look at this link for more performance considerations http://msdn.microsoft.com/en-us/data/hh949853

提交回复
热议问题