无法使用".Add()"插入新的Entity Framework实体

Aar*_*zar 1 c# silverlight entity-framework

我正在尝试将新Person数据保存到数据库中.我的代码编译得很好,但是当我运行它时,我得到一个错误.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);
}
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助,

亚伦

qJa*_*ake 6

您需要在域服务中将[Insert]方法标记为方法.它必须是public,void并且只接受一个参数(Person对象).

像这样的东西:

[Insert]
public void InsertPerson(Person p)
{
    ObjectContext.Persons.AddObject(p);
}
Run Code Online (Sandbox Code Playgroud)

此代码可能不完全是您所需要的,因为我不知道您的域名服务是什么样的,但您可以获得一般的想法.

您实际上不需要直接调用此服务方法,RIA服务链接处理此方法与Persons.Add()客户端调用之间的转换.它必须存在,就是这样.