在实体框架中处理对象上下文4 [英] Disposing of object context in entity framework 4
问题描述
我已经创建了一个存储库,它具有使用实体对象的单个实例与数据库交互的各种方法。 / p>
自动生成的类
public partial class DevEntities:ObjectContext
{
public const string ConnectionString =name = DevEntities;
public const string ContainerName =DevEntities;
资料库类
code> DevEntities db = new DevEntities();
public Customer GetCustomerByID(int id)
{
var customers = db.Customers.FirstOrDefault(c => c.CustomerId == id);
返回客户;
}
public Customer GetCustomerByPasswordUsername(string email,string password)
{
var customers = db.Customers.FirstOrDefault(c => c.Email ==电子邮件& c.Password ==密码);
返回客户;
}
从这里可以看到我对数据库实例进行多次引用。我的问题是,我最好在每个方法中实例化一个新的DevEntity,从而能够实现using语句,因此确保正确的处理,或者我现在的实现是否正常?
我也将在Repository类中实现 IDisposable
,因此它可以处理 ObjectContext
。如果每次返回不同的ObjectContext,那么在这些对象之间进行查询时,您可能遇到问题,因为这些对象被附加到不同的 ObjectContext
s,这将导致异常。
定义:
public class Repository:IDisposable
{
DevEntities db = new DevEntities();
public Customer GetCustomerByID(int id)
{
var customers = db.Customers.FirstOrDefault(c => c.CustomerId == id);
返回客户;
}
public Customer GetCustomerByPasswordUsername(string email,string password)
{
var customers = db.Customers.FirstOrDefault(c => c.Email ==电子邮件& c.Password ==密码);
返回客户;
}
public void Dispose()
{
db.Dispose();
}
}
用法:
using(Repository r = new Repository())
{
//与您的存储库进行通信
}
执行此操作,您的存储库负责处理 ObjectContext
你使用它。
I have an entity class which is auto generated from my database model. This class inherits the ObjectContext which inturn inherits IDisposable.
I have created a repository that has various methods which use a single instance of the entity object to interact with the database.
Auto generated class
public partial class DevEntities : ObjectContext
{
public const string ConnectionString = "name=DevEntities";
public const string ContainerName = "DevEntities";
Repository Class
DevEntities db = new DevEntities();
public Customer GetCustomerByID(int id)
{
var customers = db.Customers.FirstOrDefault(c => c.CustomerId == id);
return customers;
}
public Customer GetCustomerByPasswordUsername(string email, string password)
{
var customers = db.Customers.FirstOrDefault(c => c.Email == email && c.Password == password);
return customers;
}
From this you can see that I make multiple references to the db instance. My question is, am I better to instantiate a new DevEntity within each method, thus being able to implement the using statement, and so ensure correct disposal, or is my current implementation ok?
I would implement IDisposable
on the Repository class as well, so it can dispose the ObjectContext
. If you return a different ObjectContext each time, you can run into problems when doing queries between those objects, as those are attached to different ObjectContext
s, which will result in an exception.
Definition:
public class Repository : IDisposable
{
DevEntities db = new DevEntities();
public Customer GetCustomerByID(int id)
{
var customers = db.Customers.FirstOrDefault(c => c.CustomerId == id);
return customers;
}
public Customer GetCustomerByPasswordUsername(string email, string password)
{
var customers = db.Customers.FirstOrDefault(c => c.Email == email && c.Password == password);
return customers;
}
public void Dispose()
{
db.Dispose();
}
}
Usage:
using(Repository r = new Repository())
{
//do stuff with your repository
}
Doing this, your repository takes care of disposing the ObjectContext
after you used it.
这篇关于在实体框架中处理对象上下文4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!