无法在WCF Ria中使用域服务类中的方法
本文关键字:服务 方法 WCF Ria | 更新日期: 2023-09-27 18:24:45
我正在尝试构建一个silverlight应用程序,该应用程序在文本框中包含用户名、姓氏、通行证和电子邮件地址,然后将它们添加到数据库中。
为此,我使用WCF Ria Services
。
我遵循的步骤是:
在我的项目中(在web部分)添加了ADO.NET Entity Data Model
和Domain Service class
。
现在,我在DomainService类中获得了一些预定义的方法,如Insert、Update方法。我知道如何在DataGrid
中显示数据,但这不是我想要的。
我想要的是为定制所有这些
当用户单击提交按钮时,应该有一个类似AddInfo(all parameters)
的方法,它可以将所有数据添加到我的sql server数据库{目前为LocalHost}
简单地说,通过自定义方法访问数据库,使用WCF Ria Services在sql server中添加数据
我知道在.net表单中工作时很简单。但是Silverlight&WCF ria?
请提出建议。
简单地说,通过自定义方法访问数据库以添加使用WCF Ria Services 的sql server中的数据
您应该做的是在服务器端编写一个自定义方法。
在服务器端,您有一个DomainService
类,它应该继承自LinqToEntitiesDomainService<TContext>
。
只需在这个类中添加一个具有Invoke
属性的方法,例如:
[Invoke]
public void AddNewUser(string name, string firstName, int age)
{
// Put logic here to add the user to the DB
}
将用户添加到数据库的逻辑非常简单,只需创建一个新的Entity
,将其添加到上下文并调用context.SubmitChanges();
当您编译客户端RIA服务项目时,与DomainService
相对应的自动生成的代理类将包含您的新方法,您可以使用调用它
yourDomainContext ctx = new yourDomainContext();
ctx.AddNewUser("dsd", "ds", 42).Completed += (sender, e) =>
{
// Called asynchronously when the job is done
};
如果您的域服务上已经有Insert方法,您应该能够从客户端调用:
//add your new data to the context
MyDomainServiceContext.Entity.Add(myEntity); //(where "Entity" is your entity Type)
//send all the changes to the server
MyDomainServiceContext.SubmitChanges();