0

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
}

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thats mean, don't create a singleton, simple use repisotory?
Yes, this is exactly what I mean.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.