public class SingletonContext<TContext> where TContext : DbContext, new()
{
private static TContext _db;
private static object _lockSync = new object();
public SingletonContext()
{
}
public static TContext CreateContext()
{
if (_db == null)
{
lock (_lockSync)
{
_db = new TContext();
}
}
return _db;
}
}
I apply singleton pattern for dbcontext in my project like that because when i want update operation my entity throws multiple references exception.I chanced my code and problem is solved.Anyway actually
the question is different. I trying update to my entity but i didnt because modelstate is not valid.No problem so for.
I dont understand that when i will select my entity from db returned to me like that
But my entity not seted null ,as you can see.
view of my entity in db after update
The method below select operation in Data Accsess layer
public TEntity Get(Expression<Func<TEntity, bool>> filter)
{
TContext context = SingletonContext<TContext>.CreateContext(); //i called my static method that create singleton dbcontext
return context.Set<TEntity>().SingleOrDefault(filter);
}
what i want to learn is does it singleton pattern work like that? I expects my entity return to me with value. Not null. Is there something wrong i did something? or Singleton pattern work in Entity Framework like that?
Aucun commentaire:
Enregistrer un commentaire