简体   繁体   English

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

[英]Can't insert new Entity Framework entity using “.Add()”

I am trying save a new Person to the database. 我正在尝试将新的Person保存到数据库。 My code compiles just fine but when I run it I get an error at at the .Add() . 我的代码可以很好地编译,但是当我运行它时,在.Add()处出现错误。

The error says, " This EntitySet of Type 'Diavik.DataAccess.Person' does not support the 'Add' operation. " 该错误显示:“ This EntitySet of Type 'Diavik.DataAccess.Person' does not support the 'Add' operation.

This is a SilverLight application and this file is the App.xaml.cs . 这是一个SilverLight应用程序,此文件是App.xaml.cs

Here is my code: 这是我的代码:

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);
}

Thank you for your help, 谢谢您的帮助,

Aaron 亚伦

You need to have a method in your Domain Service marked as an [Insert] method. 您需要在域服务中将一个方法标记为[Insert]方法。 It must be public , void , and take only one parameter (the Person object). 它必须是publicvoid ,并且只能接受一个参数( Person对象)。

Something like this: 像这样:

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

This code may not be exactly what you need because I don't know what your domain service looks like, but you get the general idea. 这段代码可能并不是您所需要的,因为我不知道您的域服务是什么样子,但是您可以大致了解。

You don't actually need to call this service method directly, the RIA Services link handles the translation between this method and when you call Persons.Add() on the client side. 实际上,您实际上不需要直接调用此服务方法,RIA Services链接可处理该方法与在客户端上调用Persons.Add()时的转换。 It just has to exist, that's all. 它只是必须存在,仅此而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM