Good practice declaring a database variable

In most cases, when I work with an object associated with a database, I work as follows:

// Declare a private static variable of my database
private static BlueBerry_MTGEntities mDb = new BlueBerry_MTGEntities();

Then in any method (example):

public StoreInfo GetStoreByID(int _storeID)
{
    using (mDb = new BlueBerry_MTGEntities())
    {
        mDb.Database.Connection.Open();

        // Bla bla stuff to return the proper StoreInfo Object.
    }
}

Is it good practice to work in such a way as to avoid failures during failures and to develop an effective MVC Asp.Net application? If not, what will be good practices and how will you do it?

+4
source share
2 answers

You should not do it like this - this is a misuse of the function static.

, mDb , StoreInfo, using. using, mDb , , . , using, .

mDb , using, :

using (var mDb = new BlueBerry_MTGEntities())
{
    mDb.Database.Connection.Open();

    // Bla bla stuff to return the proper StoreInfo Object.
}
+4

( ), using.

public StoreInfo GetStoreByID(int _storeID)
{
    using (BlueBerry_MTGEntities mDb = new BlueBerry_MTGEntities())
    {
        mDb.Database.Connection.Open();

        // Bla bla stuff to return the proper StoreInfo Object.
    }
}

mDb using, , ( ).

+5

All Articles