Question

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
}
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top