isxaker
isxaker

Reputation: 9496

How using DataContext from singleton in repository?

Ef generate MyDataModel.edmx with class MyDataEntities for my database MyData. I create a singleton class SingletonMyDataContext

public sealed class SingletonMyDataContext
    {
        private static readonly MyDataEntities_instance = new MyDataEntities();
        private SingletonMyDataContext() { }
        static SingletonMyDataContext() { }

        public static MyDataEntitiesInstance { get; private set; }
    }

I want to create a Repository class for work with data in DB, but i don't now, how to use in repository context(MyDataEntities _instance), because after using need call dispose(). How using DataContext from singleton in repository?

public class Repository
{
private DbContext _context = SingletonMyDataContext.Instance;

//logic for work with data
}

Upvotes: 0

Views: 728

Answers (1)

Dennis
Dennis

Reputation: 37780

How using DataContext from singleton in repository

Obviously, you should not make your data context as a singleton. Moreover, in most cases it's a bad practice. EF data context caches metadata itself, after first creation of context instance. Don't try to do it twice.

Upvotes: 1

Related Questions