DataContext 对象并为其分配属性 [英] The DataContext object and assigning its properties

查看:25
本文介绍了DataContext 对象并为其分配属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之,我希望能够将 IEnumerable 分配给我的 Context.Customers 属性(这是从 System.Data.Linq.Table 类型的 SQL 导入的表;Customer 是一种实体类型).

现在您可能想知道的第一件事是为什么要这样做?"是的,通常有更好的方法可以做到这一点,但我不是在一般情况下操作的.对我的项目的限制意味着我无权访问项目的网络"部分.因此,我将使用 IIS 中托管的 WCF 服务来解决这个问题.该服务持有 DataContext 对象并调用数据库,并与 Silverlight 应用程序进行通信.

Silverlight 应用程序调用 GetCustomersAsynch,然后等待完成的事件,此时它可以使用 Customer 实体的集合.我的问题是关于提交这些更改.我可以将更改后的集合发送回 WCF 服务,但 DataContext 对象的Customers"属性是只读的,因此我不能只将一个分配给另一个.我也不能将它们一个一个地写下来,因为可能添加了一个新条目(或者,可能是新条目).我想要的是使用传递的集合更新 DataContext 的客户属性,然后我将调用 DataContext 上的 Submit 将更改提交到数据库.

如果有人有任何建议或想法,请告诉我.

提前致谢.

解决方案

您可以在 WCF 服务中使用静态 DataContext,但通常不鼓励这样做.然后将整个对象从 Silverlight 发送到 WCF 服务方法.在 WCF 服务方法中,调用 staticCtx.UpdateObject(customer);和 SubmitChanges().如果您使用相同的 staticCtx 加载客户对象,那么 UpdateObject 会将状态更改为已更改,并且 submitChanges 将通过引擎将其发送以将其转换为更新 SQL.静态上下文可能有几个副作用,所以要小心.

另一种选择是使用 WCF 数据服务或 WCF RIA 服务(L2S 工作)和实体框架.然后就可以在客户端实际调用SubmitChanges,使用LINQ从客户端查询.

这是众多 RIA 服务之一 文章和 WCF 数据服务.如何在两者之间做出选择,有很多考虑.

The short story is that I would like to be able to assign an IEnumerable to my Context.Customers property (this being table imported from SQL of type System.Data.Linq.Table; Customer being an entity type).

Now the first thing you might wonder is 'why would you do this?' Yes, ordinarily there are better ways to do this, but I am not operating under ordinary circumstances. A limitation on my project means I do not have access to the 'web' part of the project. As a result, I'm going about this using a WCF service hosted in IIS. This service holds the DataContext object and makes the calls to the database, and it communicates with the Silverlight application.

The Silverlight application calls GetCustomersAsynch and then waits for the completed event, at which point it can use a collection of Customer entities. My problem is about submitting those changes. I can send the altered collection back to the WCF service, but the 'Customers' property of the DataContext object is read only so I can't just assign one to the other. I can't just write them over one for one either, because there might have been a new entry added (or, potentially, new entries). What I want is for the DataContext's Customers property to be updated with the passed collection, and then I will call Submit on the DataContext to submit the changes to the database.

If anyone has any suggestions or ideas, please let me know.

Thanks in advance.

解决方案

You could have a static DataContext in your WCF servic, though it is usually discouraged. Then send the whole object from Silverlight to the WCF service method. Inside the WCF service method, call staticCtx.UpdateObject(customer); and SubmitChanges(). If you loaded the customer object with the same staticCtx, then UpdateObject will change the state to changed and submitChanges will send it through the engine to convert it to update SQL. There can be several side-effects of a static context, so beware.

Another option, is to use WCF Data Services or WCF RIA Services (L2S works) and Entity Framework. Then you can actually call SubmitChanges on the client and use LINQ to query from the client.

Here is one of many RIA Services articles that are available and one for WCF Data Services. There are a lot of considerations on how to the make the choice between the two.

这篇关于DataContext 对象并为其分配属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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