开发者

Access to Model in Entity Framework

开发者 https://www.devze.com 2023-03-09 10:53 出处:网络
What is the problem of idea that we have a static property of our entity model like this? public class Repository{

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;
 开发者_JS百科    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();
     }
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消