Entity Framework foreign keys are not updated appropriately.

c# ef-code-first entity-framework

Question

Foreign key relationships in the entity structure are giving me problems. I've got two tables.Persons and Countries . Persons Includes a column for foreign keys.CountryId .

As theCountries table doesn't change often, thus I simply want to get its data once and discard it.DatabaseContext and maintain a list ofCountries cached in a location. Where I'm having trouble is here.

According to the entity framework, you should open a database context, add or change rows as necessary, and then shut the database context. It has issues if you open, get data, then shut, and then later open, store data, then close.

So, here are my POCO objects:

public class Country {
    public int CountryId {get; set; }
    public String Name {get; set; }
}

public Person {
    public int PersonId {get; set; }
    public virtual Country Country {get; set; }
}

Then I attempt to make a new character that looks like this:

Country[] countries;
using (var dt = new DatabaseContext())
{
    countries= dt.Countries.ToArray();
}

Person person = new Person();
person.Country = countries[0];
using (var dt = new DatabaseContext()) { 
    dt.Entities.Add(person);
    dt.SaveChanges();
}

The entity framework adds a new record to the database upon saving.Countries the same-named table ascountries[0] but a fresh, increased ID. Obviously, this is not the intended result; the individual should insteadCountry_CountryId field with the value ofcountries[0] , and no additional rows ought to be added.

How can I fix this? I believe that forcing the entity framework to build a new row whenever it is presented with an object that already has its main key set would be a workable option. Exists a method to do this?

1
1
4/16/2012 10:34:16 AM

Accepted Answer

Since this is a pretty frequent topic posed every few days, I wonder if you at least did a little research on the Internet before you put quite a lot of work into describing the problem.

Add Every entity in the entity graph is added using the technique. then if you linkcountry to person and country not relevant to the issue at hand, callingAdd on theperson must tally bothperson and country as potential new entities. If you choose not tocountry EF must be informed in order to be inserted.country is not a fresh concept:

Person person = new Person();
person.Country = countries[0];
using (var dt = new DatabaseContext()) { 
    dt.Entities.Add(person);
    dt.Entry(person.Country).State = EntityState.Modified;
    dt.SaveChanges();
}
3
4/16/2012 10:44:12 AM

Popular Answer

I've included the two approaches below to my strategy to fix this issue:DatabaseContext class:

public void Add(object target)
{
    this.Set(target.GetType()).Attach(target);
    this.Entry(target).State = System.Data.EntityState.Added;
}

public void Modify(object target)
{
    this.Set(target.GetType()).Attach(target);
    this.Entry(target).State = System.Data.EntityState.Modified;
}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow