2012-02-15 48 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();