none
EntityFramework performs update while insert is expected RRS feed

  • Question

  • I'm using the entity framework to try and insert new persons into a database.
    Because we need normalized data, all details are split in different objects generated through entity.
    However, i'm haveing some strange behaviour here. When using debug it follows the correct flow, however when saving, i get following error.

    "The UPDATE statement conflicted with the FOREIGN KEY constraint \"FK_AddressPerson\". The conflict occurred in database \"dewaele\", table \"dbo.Address\", column 'addressGUID'.\r\nThe statement has been terminated."}
    The code is as followed, where 'context' is a class that handles my connection with EF
    newPerson.PersonDataSource.Add(pds);
            context.addPerson(newPerson);
            context.saveChanges();
            newPerson.addAddress(context, person.street, person.streetNumber, person.streetBus, person.city, person.zipCode, person.country, person.region, person.province);
            context.saveChanges();
            
    the first

     

    context.saveChanges();

    is executed without problem, so i see the person appearing in the database, the problem happens in the second, then the error is thrown.

    the code of newPerson.addAddress()

    ad = new Address();
          this.Address = ad;
          ad.addCity(context, cityName, zipCode, countryName, regionName, provinceName, this.LanguageReference.Value);
          City c = ad.getCity();
          if(!String.IsNullOrEmpty(streetNumber))
          ad.number = streetNumber.ToLower();
          if(streetBus != null)
            ad.bus = streetBus.ToString();
          ad.addStreet(context, streetName, c, this.LanguageReference.Value);
    I have tried putting the addCity and addStreet in comments, so i'm just left with the regular attributes of the address object. But this changed nothing to the situation.
    Any reason why entity would try to do an update instead of an insert?
    Tuesday, April 5, 2011 2:41 PM

Answers

  •  

    Hi Stannie79,

    Thanks for your feedback!

    The relationship between the Person and Address is M:1. There is foreign key "AddressId" in your Person table.  When you create a person and save it to the database, the AddressId is empty. Now you want to insert a new Address and update the Person table's AddressId, right?

    ////// Employee: Vacation=1:M------1. insert a vacation without employeeid, 2.insert an employee 3.update vacation's employeeid.
     Vacation v = new Vacation() { VactionId=1, Days=3 };
            context.Vacations.AddObject(v);
            context.SaveChanges();
             Employee e = new Employee() {EmployeeId=1, Email = "efsafe", ModifyTime = DateTime.Now, Phone = "1faf31", Name = "hello" };
             e.Vacations.Add(v);
             context.Employees.AddObject(e);
            context.SaveChanges();
    

     

    Let me know your opinion, thanks.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Stannie79 Tuesday, April 12, 2011 12:04 PM
    Monday, April 11, 2011 8:59 AM
    Moderator

All replies

  •  

    Hi Stannie79,

    Welcome!

    According to your description and code, the exception is very strange, do you want to insert the two records(Person and Address)? What's the relationship between the two tables. If it is Person:Address=1:M. I think you can use Navigation property to insert, here is my sample:

    =====================================================

    Employee: Vacation=1:M

    //add two new entities

      var v= new Vacation(){ VactionId=2, Days=3};

                    var e = new Employee() { EmployeeId=2, Email="jsj", ModifyTime=DateTime.Now, Name="Alice", Phone="121" };

                    e.Vacations.Add(v);

                    context.AddToEmployees(e);

                    context.SaveChanges();

    //another way

     var v= new Vacation(){ VactionId=3, Days=3};

                    var e = new Employee() { EmployeeId=3, Email="jsj", ModifyTime=DateTime.Now, Name="Alice", Phone="121" };

                    v.Employee = e;

                    context.Vacations.AddObject(v);

                    context.SaveChanges();

    =======================================================

    >>>>>>>>>>all details are split in different objects generated through entity.

    I'm not clear about this, but I think you can create a new object and set properties from the entity.

     

    By the way, The method in your post is hard to understand, would you please give me more code, or ping me:  v-alache@microsoft.com

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Wednesday, April 6, 2011 6:24 AM
    Moderator
  •  

    My apolagies

    var v= new Vacation(){ VactionId=2, Days=3};

                    var e = new Employee() { EmployeeId=2, Email="jsj", ModifyTime=DateTime.Now, Name="Alice", Phone="121" };

                    e.Vacations.Add(v);

                    context.AddToEmployees(e);

                    context.SaveChanges();

     

    woud be a god example of how i'm tryingto do so.

     

    So i have an object Person

    1 Person has 1 Address, 1 Address can have multiple Person.

     

    Now what i do, a create a person, save it to the databass without address.

    Next in the Person object i have a mehod callled addAddress.

    In that methode do something like

    this.Address  = new Address();

    so the above codeis inside the Person object.

    When i then try to save my person again, it tries to do an update on the address column in the Address table of the database, intead of doing an insert. Which is strange cause befoe ading an address, in the persons table of the database we have NULL for person.Address, so there's no relation. I'll send you the whole  4 files with code later today.


    Wednesday, April 6, 2011 10:02 AM
  •  

    Hi Stannie79,

    Thanks for your feedback!

    The relationship between the Person and Address is M:1. There is foreign key "AddressId" in your Person table.  When you create a person and save it to the database, the AddressId is empty. Now you want to insert a new Address and update the Person table's AddressId, right?

    ////// Employee: Vacation=1:M------1. insert a vacation without employeeid, 2.insert an employee 3.update vacation's employeeid.
     Vacation v = new Vacation() { VactionId=1, Days=3 };
            context.Vacations.AddObject(v);
            context.SaveChanges();
             Employee e = new Employee() {EmployeeId=1, Email = "efsafe", ModifyTime = DateTime.Now, Phone = "1faf31", Name = "hello" };
             e.Vacations.Add(v);
             context.Employees.AddObject(e);
            context.SaveChanges();
    

     

    Let me know your opinion, thanks.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Stannie79 Tuesday, April 12, 2011 12:04 PM
    Monday, April 11, 2011 8:59 AM
    Moderator
  •   Now you want to insert a new Address and update the Person table's AddressId, right?

    Indeed, i have been thinking about the above solutions aswell.
    Though it seemed a dirty way of doing things. Shouldn't entity be able to do an insert and update at the same time?

    I'll try the above propsed solution later on, but any input as to why an update isn't possible in this situation would be welcome.

    Monday, April 11, 2011 12:24 PM