处理实体框架中的上下文

时间:2009-11-06 19:14:37

标签: c# .net asp.net entity-framework

我有一个为实体提供CRUD操作的类。我使用上下文作为私有成员,可以访问类中的所有方法。

  public class CustomerService
  { 
    private CeoPolandEntities context;

    public CustomerService()
    {
        context = new CeoPolandEntities();
    }


    public bool IsCustomerValid(string userName,string password)
    {
        Customer customer;

        customer = context.CustomerSet.FirstOrDefault(c => c.UserName == userName
                                                    && c.Password == password);

        return customer == null ? false : true;
    }

    public bool IsUserNameValid(string userName)
    {
        Customer customer;

        customer = context.CustomerSet.FirstOrDefault(c => c.UserName == userName);

        return customer == null ? true : false;
    }
}

这是正确使用上下文??它的线程安全和安全吗?

它是一个ASP.NET应用程序。

2 个答案:

答案 0 :(得分:1)

只要您有不同的CustomerService实例来处理不同的请求,您就不必担心这一点。如果您碰巧有自己创建的任何线程,请避免在同一实例中调用多个方法。

答案 1 :(得分:1)

上下文不是线程安全的。

如果符合以下条件,您当前的代码就可以了:

  1. 您将CustomerService更改为Dispose the Context。
  2. 每个请求使用一个CustomerService。