0

What is the problem of idea that we have a static property of our entity model like this?

public class Repository{

       private static KiaNetEntities entities = null;
       public static KiaNetEntities{
           get{ return entities; }
       }

       static Repository(){
           entities = new KiaNetDbEntities();
       }
}

and use it like this:

public static Customers[] GetCustomers(){
     var q = from c in KiaNetEntities.Customers where c.Activated select c;
     return q.ToArray();
}

public static Customers[] AddToCustomerSalary(int customerId, decimal newValue){
     var q = from c in KiaNetEntities.Customers 
     where c.Activated && c.ID == customerId
     select c;

     if(q.Count() > 0){
              var customer = q.First();
              customer.Salary += newValue;
              KiaNetEntities.SaveChanges();
     }
}
Jalal
  • 6,097
  • 9
  • 59
  • 95

1 Answers1

1

What is the problem? There are quite lot of them - some are described here and you can add one more - EF classes are not thread safe so sharing single context among all requests in your web application is going to hell. Context and its internals are not stateless so simply sharing them is very bad idea.

Community
  • 1
  • 1
Ladislav Mrnka
  • 349,807
  • 56
  • 643
  • 654