no

How to Insert or Update a Table With Foreign Key in Entity Framework

For example you have 2 lookup entities: Country and Province And Your Person Entity has country and province field. In short both Country...

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();

Related

c# 6507114392198544641

Post a Comment Default Comments

Outsourcing

Are you looking for freelancers in the Philippines? Get in touch.

Donations

If you like what I do, you can support this channel by buying me a coffee. I would be grateful for your contribution! Your donations will help me create more FREE online courses and learning materials for programmers and remote workers.

item