我什么时候可以处置我的 DataContext?
举个例子:
MyDataContext context = new MyDataContext(); // DB connection established.
MyTableRecord myEntity = myDataContext.Table.FindEntity(12345); // retrieve entity
假设我的实体与其他表有关系,我可以通过
foreach (MyEntity.RelatedTable 中的 var record) 访问这些
表,我是否需要在第二行之后保持 DataContext 处于活动状态才能访问实体的财产或者是否足够安全可以处置?
我了解 Linq to SQL 使用延迟执行,因此我想知道它是否仅在您最初检索实体时使用延迟执行,还是在访问相关表记录时也使用延迟执行。
示例
var userRepo = new UserRepository(); // creates new DataContext
var auditRepo = new AuditRepository(); // creates new DataContext
var activeUsers = userRepo.FindActiveUsers();
foreach (var user in activeUsers)
{
// do something with the user
var audit = new Audit();
audit.Date = DateTime.Now;
audit.UserID = user.ID;
auditRepo.Insert(audit);
}
我的存储库中的插入方法调用 SubmitChanges
。那么上面的情况是否可以接受,或者这是浪费连接。我实际上应该这样做:
var userRepo = new UserRepository();
var activeUsers = userRepo.FindActiveUsers();
foreach (var user in activeUsers)
{
// do something with user
var audit = new Audit();
audit.Date = DateTime.Now;
audit.UserID = user.ID;
user.Audits.Add(audit);
userRepo.Save();
}
重新使用已经打开的 DataContext 吗?在打开高级数据上下文然后必须进行一些低级处理的情况下,您会做什么,我应该传递 userRepo 还是应该创建一个单独的存储库?
Take the following example:
MyDataContext context = new MyDataContext(); // DB connection established.
MyTableRecord myEntity = myDataContext.Table.FindEntity(12345); // retrieve entity
Say my entity has relationships with other tables which I would access via
foreach (var record in MyEntity.RelatedTable)
Do I need to keep my DataContext alive after the 2nd line in order to access the properties of the entities or is it safe enough to dispose of?
I understand Linq to SQL uses delayed execution hence I am wondering if it only uses delayed execution when you initially retrieve the entity or whether it uses this when accessing the related table records aswell.
Example
var userRepo = new UserRepository(); // creates new DataContext
var auditRepo = new AuditRepository(); // creates new DataContext
var activeUsers = userRepo.FindActiveUsers();
foreach (var user in activeUsers)
{
// do something with the user
var audit = new Audit();
audit.Date = DateTime.Now;
audit.UserID = user.ID;
auditRepo.Insert(audit);
}
My insert method in my repo calls SubmitChanges
. So is the above acceptable, or is this a waste of a connection. Should I realistically do:
var userRepo = new UserRepository();
var activeUsers = userRepo.FindActiveUsers();
foreach (var user in activeUsers)
{
// do something with user
var audit = new Audit();
audit.Date = DateTime.Now;
audit.UserID = user.ID;
user.Audits.Add(audit);
userRepo.Save();
}
To re-use the already open DataContext? What would you do in situations where you open a high-level datacontext and then had to do some processing low level, should I pass the userRepo down or should I create a separate Repository?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要访问其他表中的字段,您需要保持 DataContext 处于活动状态。另外,不要直接调用dispose,使用using关键字。
To access fields in other tables you will need to keep the DataContext alive. Also, don't call dispose directly, use the using keyword.
您不需要处置 DataContext,因为它是一个轻量级对象,并且所有内部数据库连接都被缓存。如果您确实处置它,您将无法访问相关实体。
You don't need to dispose of the DataContext as it's a lightweight object and all internal db connections are cached. If you do dispose of it you won't be able to access the related entities.