2011-05-30 91 views
0

我們的實體模型有這樣的static屬性的想法有什麼問題?在實體框架中訪問模型

public class Repository{ 

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

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

,並使用它像這樣:

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(); 
    } 
} 
+0

不知道你的問題是什麼。爲什麼「GetCustomers」和「AddToCustomerSalary」方法是靜態的? – IndigoDelta 2011-05-30 12:45:48

回答

1

問題是什麼?其中有相當多的 - some are described here,你可以再添加一個--EF類不是線程安全的,因此在你的web應用程序的所有請求之間共享單個上下文將會惡化。上下文及其內部並不是無狀態的,因此簡單地分享它們是非常糟糕的主意。