Updating LinqDataSource from separate DataContext

老子叫甜甜 提交于 2020-01-25 03:55:08

问题


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.

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


回答1:


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

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
}


来源:https://stackoverflow.com/questions/924312/updating-linqdatasource-from-separate-datacontext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!