2012-02-15 72 views
0

我正在尝试构建一个Silverlight应用程序,它将文本框中的用户名,姓氏,通行证,电子邮件地址添加到数据库中。无法使用WCF Ria中域服务类的方法?

为此,我正在使用WCF Ria Services

步骤我都遵循如下:

新增ADO.NET Entity Data Model,然后在我的项目Domain Service class(在网络的一部分)。

现在我已经有一些预定义的方法在我的DomainService类中,比如Insert,Update方法。我知道如何在DataGrid中显示数据,但它不是我想要的。

我要的是自定义这一切:

当用户点击提交按钮,那么就应该有这个喜欢AddInfo(all parameters)里面方法,都可以将数据添加到我的SQL Server数据库{目前本地主机} 。

在简单的话访问通过自定义方法的数据库中添加使用WCF RIA服务

我知道这是很简单的在.NET的形式和所有的工作,而在SQL Server中的数据。但是Silverlight & WCF ria呢?

请推荐。

回答

1

在通过定制方式访问数据库的简单的话在SQL添加 数据服务器使用WCF Ria服务

你应该做的是在服务器端编写一个自定义方法。

在服务器端,您有一个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 Services项目,自动生成的代理类,符合您的DomainService将包含新的方法,你就可以使用称之为:

yourDomainContext ctx = new yourDomainContext(); 
ctx.AddNewUser("dsd", "ds", 42).Completed += (sender, e) => 
{ 
    // Called asynchronously when the job is done 
}; 
1

如果你已经在你的域名服务的插入方法,你应该能够从客户端调用:

//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();