2011-04-17 40 views
0

我已經創建了一個數據庫,並將其與我的Silverlight應用程序中的DomainService進行了關聯。現在我想通過使用服務來執行某些操作,例如註冊,登錄等。執行SQL查詢WCF RIA Silverlight

我該如何做到這一點。我有在服務中創建的預設方法,例如InsertUser,但它只需要一個參數,所以我不知道它是如何工作的。在元數據我有所有領域等

任何人都可以幫我在這裏。

謝謝。

public IQueryable<User> GetUsers() 
     { 
      return this.ObjectContext.Users; 
     } 

public void InsertUser(User user) 
     { 
      if ((user.EntityState != EntityState.Detached)) 
      { 
       this.ObjectContext.ObjectStateManager.ChangeObjectState(user, EntityState.Added); 
      } 
      else 
      { 
       this.ObjectContext.Users.AddObject(user); 
      } 
     } 

用於獲取用戶我使用(從TBohnen.jnr代碼庫):

UserContext _userContext = new UserContext(); 

     public MainPage() 
     { 
      InitializeComponent(); 
      LoadOperation loGetUsers = _userContext.Load(_userContext.GetUsersQuery()); 
      loGetUsers.Completed += new EventHandler(loGetUsers_Completed); 
     } 

     void loGetUsers_Completed(object sender, EventArgs e) 
     { 
      LoadOperation<Web.User> lo = (LoadOperation<Web.User>)sender; 
      var user = _userContext.Users; 
      MessageBox.Show(user.ToString()); 
     } 
+0

請從您的域服務添加示例方法,它是IQuerable還是Invoke操作? – 2011-04-17 15:19:34

+0

添加了生成的代碼 – 2011-04-17 15:22:01

+0

酷,給了一個答案,讓我知道如果你需要別的 – 2011-04-17 15:29:41

回答

2

這是增加一個新用戶:

YourDomainContext dc = new YourDomainContext(); 
User userToAdd = new User(); 
//You will have to set your properties here as I don't know them, I will give an example. 
userToAdd.username = "NewUser"; 
dc.User.Add(userToAdd); 
dc.SubmitChanges(); 

要檢索現有的用戶:

YourDomainContext dc = new YourDomainContext(); 
LoadOperation loGetUsers = dc.Load(dc.GetUsersQuery()); 
loGetUsers.Completed += new EventHandler(loadOperation_Completed);// You will see there is a callback overloads as well 

and then add this as well. 

private void loadOperation_Completed(object sender, EventArgs e) 
{ 
    LoadOperation<User> lo = (LoadOperation<User>)sender; 
    //Have a look at all the properties like lo.Error etc. but to see the retrieved users you can either use: 
    var users = lo.AllEntities; 
    //or if you declared your domaincontext as a class level parameter: 
    var users = dc.User; 
    foreach (Web.User user in users) 
    { 
     MessageBox.show(user.username); 
    } 
} 

這將t需要一個異步調用來獲取所有用戶,並將其添加到DomainContext中,並且您將能夠通過dc.User訪問它。

+0

對不起,但沒有奏效,我可以得到dc.User.Add但是然後從新用戶沒有這樣的事情和我不能完成其餘的代碼。 – 2011-04-17 15:29:43

+0

好吧,會改變一下 – 2011-04-17 15:33:17

+0

好的謝謝,我會測試一次,你做了更改 – 2011-04-17 15:34:19