2013-06-12 55 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()时的转换。它必须存在,就是这样。