views:

260

answers:

4

Hello

I have a small ASP.NET MVC application with the following entity objects:

Person

  • PersonId
  • Name (string)
  • FirstName (string)
  • Country (Country)

Country

  • CountryId
  • Name

I can add and delete the entity's this works fine. I can also update name, firstname. But how can i update the country property with another country.

i was trying

p.Country = (from c in db.Country 
             where c.CountryId == countryId 
             select c).First();

but this fires an exception {"An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key."}"

even before i call SaveChanges on the datacontext.

Can someone explaind how i can update this property?

kind regards Dieter

+2  A: 

Is db your context? You should be able to do:

p.Country = ctx.Country.First(c => c.CountryId == countryId);

Or, if you don't want to query the database to get the foreign key entity you can also use an EntityKey to the same effect:

p.CountryReference.EntityKey = new EntityKey("MyDb.Country", "CountryId", countryId);
Steven Robbins
db is my contenxt, but your solution doesen't work, i get an exception on the line p.Country = db.Country.First...{"An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key."}
Dieter
Odd. Have you tried the second solution?
Steven Robbins
A: 

i used the second solution and i got no exception, but the CountryId wasen't changed in the database even afther i called AcceptChanges

Hello Davyou code works for new countries, i want to update a country propertie with a other existing country.but thx anywayregardsdieter
@Deiter, in the @theNewCountryID put the ID of the existing country in the countries table that you want to update the Person record to.Maybe that was a bad choice of variable names!
DaveB
still got exception - {"An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key."} System.Exception {System.InvalidOperationException}wich is normal because i attach the country wich is already know by the object statemanager
Perhaps you can post your code so we can detect the problem.
DaveB
public void UpdateRSVP2(GuestResponse updatedResponse, int countryId) { updatedResponse.EntityKey = (from response in db.GuestResponse where response.GuestResponseId == updatedResponse.GuestResponseId select response).FirstOrDefault().EntityKey; updatedResponse.Country = (from c in db.Country where c.CountryId == countryId select c).First(); db.ApplyPropertyChanges(updatedResponse.EntityKey.EntitySetName, updatedResponse); db.SaveChanges();
Here is how I have been updating Navigation properties. If you don't have any scalar properties to update you can skip the ApplyPropertyChanges call. origResponse = (from r in db.GuestResponse where r.GuestResponseId == updatedResponse.GuestResponseId select r).FirstOrDefault(); Country country = new Country { CountryID = countryId }; db.AttachTo("CountriesSet", country); origResponse.Country = country db.ApplyPropertyChanges("GuestResponseSet", updatedResponse); db.SaveChanges();
DaveB
A: 

Code similar to this has worked for me when updating navigation properties.

   Country country = new Country{CountryId = theNewCountryID };
    db.AttachTo("Countries", country);
    p.Country = country;
    db.Savechanges();

Create a stub of the new country then attach it to the countries EntitySet, assign the new country to your Entity's navigational country property and call SaveChanges(). Use your country EntitySet name in the AttachTo call.

DaveB
A: 

This is what worked for me. In the model:

<%= Html.Textbox("Country.CountryId", Model.Countries) %> // I'm using a form model view

In the controller:

Person originalPerson = (from p in db.PersonSet
                        where p.PersonId == updatedPerson.PersonId
                        select p).First();

Country country = (from c in db.CountrySet 
                  where c.CountryId == updatePerson.Country.CountryId 
                  select c).First();

db.Attach(country);
originalPerson.Country = country;
db.ApplyPropertyChanges(originalPerson.EntityKey.EntitySetName, updatedPerson);
db.Savechanges();
Becky