从单独的DataContext更新LinqDataSource [英] Updating LinqDataSource from separate DataContext

查看:69
本文介绍了从单独的DataContext更新LinqDataSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用从不同DataContext检索到的记录来更新一个LinqDataSource.问题在于LinqDataSource的DataContext似乎没有公开,因此我无法访问它以将新对象附加到DataSource的上下文.

I want to update one LinqDataSource with a record retrieved from a different DataContext. The problem is that the DataContext of the LinqDataSource doesn't seem to be made public so I can't get access to it to attach the new object to the DataSource's context.

正常的做法是什么?还是应该只删除LinqDataSource

What is the normal way of doing this? Or should I just scrap the LinqDataSource

推荐答案

这样做的充分理由-您不能在不同的DataContext中共享对象.

Good reason for that - you can't share objects across different DataContexts.

如果使用LinqDataSource的OnContextCreating事件,则可以将正确的实例附加到数据源[即从第二个实体中检索到的实例],以便它们都来自同一DataContext:

If you use the LinqDataSource's OnContextCreating event you can attach the correct instance to the data source [ie, that which the second entity is retrieved from] so they're both from the same DataContext:

ASPX:

<asp:LinqDataSource .... OnContextCreating="GetDataContext" ... />

C#:

protected void GetDataContext(object sender, LinqDataSourceContextEventArgs e)
{
    e.ObjectInstance = MyDataContext.Current; // assuming that Current is the ambient instance
}

这篇关于从单独的DataContext更新LinqDataSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆