我有一个为实体提供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应用程序。
答案 0 :(得分:1)
只要您有不同的CustomerService实例来处理不同的请求,您就不必担心这一点。如果您碰巧有自己创建的任何线程,请避免在同一实例中调用多个方法。
答案 1 :(得分:1)
上下文不是线程安全的。
如果符合以下条件,您当前的代码就可以了: