简体   繁体   中英

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

I am trying save a new Person to the database. My code compiles just fine but when I run it I get an error at at the .Add() .

The error says, " 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 .

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. It must be public , void , and take only one parameter (the Person object).

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. It just has to exist, that's all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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