2013-06-12 123 views
1

我正在嘗試將新的Person保存到數據庫。我的代碼編譯得很好,但是當我運行它時,我在.Add()處得到一個錯誤。無法使用「.Add()」插入新的實體框架實體

錯誤說, 「This EntitySet of Type 'Diavik.DataAccess.Person' does not support the 'Add' operation.

這是一個Silverlight應用程序,這文件是App.xaml.cs

這裏是我的代碼:

private void OnGetPerson_Completed(LoadOperation<Person> operation) 
{ 
    Person person = operation.Entities.SingleOrDefault(); 

    if (person == null) 
    { 
     person = new Person() 
     { 
      FirstName = WebContext.Current.User.FirstName, 
      LastName = WebContext.Current.User.LastName, 
      IlluminatorLogin = WebContext.Current.User.Name 
     }; 

     Context.Persons.Add(person); 
    } 

    Context.SubmitChanges(submitOp => 
    { 
     // Some Stuff 
    }, null); 
} 

感謝你的幫助,

亞倫

+1

你的域名服務是怎樣的?你有'[Insert]'方法嗎? – nemesv

回答

5

你需要有一種方法在你的域名服務標記爲[Insert]方法。它必須是publicvoid,並且只取一個參數(Person對象)。

事情是這樣的:

[Insert] 
public void InsertPerson(Person p) 
{ 
    ObjectContext.Persons.AddObject(p); 
} 

此代碼可能不是正是你需要的,因爲我不知道您的域名服務是什麼樣子,但你的總體思路。

您實際上不需要直接調用此服務方法,RIA服務鏈接處理此方法之間的轉換以及在客戶端上調用Persons.Add()時的轉換。它必須存在,就是這樣。