我有一个类为实体提供了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应用程序。
所以,如果我首先调用一个实例的加载方法,然后保存方法,那不好?为什么? – user137348 2009-11-06 19:20:42
不,我的意思是,如果你自己创建线程(而不是为请求创建的那些asp.net),那么它的坏,也就是说,它可能仍然是加载时,已经达到保存...更常见的是,你正试图同时加载具有相同上下文实例的多个事物。它完美的k,在常规的asp.net请求期间使用相同的实例来加载内容和l8r保存。 – eglasius 2009-11-06 19:24:59