【问题标题】:Access to Model in Entity Framework访问实体框架中的模型
【发布时间】:2011-09-04 19:22:15
【问题描述】:

我们有这样的实体模型的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();
     }
}

【问题讨论】:

  • 不确定您的问题是什么。为什么GetCustomersAddToCustomerSalary 方法是静态的?

标签: c# asp.net-mvc-2 entity-framework-4 data-access-layer


【解决方案1】:

有什么问题?它们有很多——some are described here,你可以再添加一个——EF 类不是线程安全的,所以在你的 Web 应用程序中的所有请求之间共享单一上下文将会变得很糟糕。上下文及其内部不是无状态的,因此简单地共享它们是非常糟糕的主意。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-28
    • 2018-05-14
    相关资源
    最近更新 更多