0

I have a huge problem with one of our old applications. It is c# with Entity Framework. Now I have a table Person (Name, StreetName, Phone, PostCodeId etc.) and another table PostCode (Id, PostCode, CityName). I have an edit form where the user can add/edit persons. The person edit form includes a combobox for either typing or selecting the postcode.

When the user select/type a new postcode the person object is updated with a new postcode object and when the user saves, the person object is saved. The framework then updates the postcode table, changing the city name of the old postcode to the new city name. What I want it to do is to update the person table with a new postcodeid.

This is the code in the SelectIndexChanged of the postcode combo:

    private void PostCodeCombo_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (PostCodeCombo.SelectedItem != null)
        {
            person.PostCode = (BSSModel.PostCode)PostCodeCombo.SelectedItem;
            personBindingSource.EndEdit();
        }
        CityNameLabel.Text = person.PostCode.CityName;
    }

Can anyone give me some ideas where to look, or what is wrong? I am not that well into how the entity framework works. The Person table has a foreign key to the PostCode table to make the framework link these two in the datamodel. Can this foreign key be wrong somehow?

Best regards Hans Milling...

nivs1978
  • 1,126
  • 14
  • 20
  • Firstly you need to show the code that saves the entities. What you need to remember, is that EF works with object references, if you remove a reference and put a new object there, EF will create a completely new entry for it. – Callum Linington Feb 25 '15 at 08:04
  • fetch the existing postcode from the database and set that to the navigational property **or** set the id of the existing postcode on the person. Then save. – default Feb 25 '15 at 08:27
  • I have an entries object: private BSSEntities m_context; I then call the SaveChanges(); If I try to set the Id of the PostCode object, I get an InvalidOperationException: The property 'Id' is part of the object's key information and cannot be modified. – nivs1978 Feb 25 '15 at 09:12
  • I just tried to set the PostCode and CityName of the person.PostCode object from the item selected in the combobox. That results in a new row in the PostCode table in the databsae as Callum describes. Question is still how I change it on the person object without changing the existing row in the PostCode table. – nivs1978 Feb 25 '15 at 09:26
  • Another thing I don't get is why overwriting the person.PostCode object with the one from the combobox only changes the cityname in the postcode table and not the postcode. That makes no sense to me. It should not change any of them, but since it does change, why are onlye one and not both changed? – nivs1978 Feb 25 '15 at 09:55

1 Answers1

0

My solution was to create a copy of the object and delete the old one, then editing the postcode of the copy before saving. The object gets a new id then, but since no other tables point to the person table; it does not cause any problems.

nivs1978
  • 1,126
  • 14
  • 20