Looking for a JavaEE Architect or Potential Tech Co-Founder?
Please don't hesitate to contact me.

Entity Framework Inserting or Updating a Table with Foreign Key

For example you have 2 lookup entities:
Country and Province

And Your Person Entity has country and province field. In short both CountryId and ProvinceId are foreign keys to Person table.

How to setup the Insert Statement (Update is almost the same, just query the Person Entity first). Assuming we have variables countryId and provinceId.
var person = new Person {
  FirstName = "rhed",
  LastName = "czetsuya",
  Country = Entities.Country.First(p=>p.CountryId==countryId),
  Country = Entities.Province.First(p=>p.CountryId==provinceId),
}
Entities.AddToPerson(person);
Entities.SaveChanges();
Entity Framework Inserting or Updating a Table with Foreign Key Entity Framework Inserting or Updating a Table with Foreign Key Reviewed by Edward Legaspi on Thursday, February 17, 2011 Rating: 5

No comments:

Powered by Blogger.