Access to Model in Entity Framework
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.
精彩评论