Mou Mou - 1 month ago 9
C# Question

Entity Framework: How to get ID of a parent table before insert the same id in child table

My entity relationship is Customer > Address > Contacts. i am trying to update data in customer table and remove specific data from Address & Contacts tables and try to add again data in Address & Contacts tables.

customer id is FK in Address table and AddressID is FK in Contacts tables.
so when i am inserting data in contact table then i have to pass AddressID value for contact table but how do i know what is the current AddressID because i remove one Address data and insert again whose ID is require for contact table to be inserted. whole things i am trying to do in one

SaveChanges()


really i am in problem and do not understand how to solve it. here is my code.

private void button3_Click(object sender, EventArgs e)
{
using (var db = new TestDBContext())
{
var existingCustomer = db.Customer
.Include(a => a.Addresses.Select(x => x.Contacts))
.FirstOrDefault(p => p.CustomerID == 5);

existingCustomer.FirstName = "Test Customer123";

existingCustomer.Addresses.Where(a => a.AddressID == 5).ToList().ForEach(r => db.Addresses.Remove(r));
existingCustomer.Addresses.Where(a => a.AddressID == 5).SelectMany(ad => ad.Contacts).Where(c=> c.ContactID==5).ToList().ForEach(r => db.Contacts.Remove(r));

Addresses oAdrModel = new Addresses();
oAdrModel.Address1 = "test xxx";
oAdrModel.Address2 = "test xxx";
oAdrModel.SerialNo = 3;
oAdrModel.IsDefault = true;
oAdrModel.CustomerID = 5;
db.Addresses.Add(oAdrModel);
int xx = oAdrModel.AddressID;

Contacts ContactModel = new Contacts();
ContactModel.Phone = "XX-1111111-33";
ContactModel.Fax = "XX-1-1111111";
ContactModel.SerialNo = 4;
ContactModel.IsDefault = true;
//ContactModel.AddressID = 5;
db.Contacts.Add(ContactModel);

db.SaveChanges();
}
}


ContactModel.AddressID whose value is not known which causing problem.

one way i can think about that i can insert data into address table and after that i have call
SaveChanges()
then i can get address ID which i can later insert into contact table. i am looking for best suggestion and guide line to handle this issue. thanks

Full Working code



using (var db = new TestDBContext())
{
//db.Database.Log = s => MyLogger.Log("EFApp", s);

var existingCustomer = db.Customer
.Include(a => a.Addresses.Select(x => x.Contacts))
.FirstOrDefault(p => p.CustomerID == 5);

existingCustomer.FirstName = "Test Customer123";

existingCustomer.Addresses.Where(a => a.AddressID == 5).ToList().ForEach(r => db.Addresses.Remove(r));
existingCustomer.Addresses.Where(a => a.AddressID == 5).SelectMany(ad => ad.Contacts).Where(c=> c.ContactID==5).ToList().ForEach(r => db.Contacts.Remove(r));

Addresses oAdrModel = new Addresses();
oAdrModel.Address1 = "test xxx";
oAdrModel.Address2 = "test xxx";
oAdrModel.SerialNo = 3;
oAdrModel.IsDefault = true;
oAdrModel.CustomerID = 5;
db.Addresses.Add(oAdrModel);
db.SaveChanges();
int CurAddressID = oAdrModel.AddressID;

Contacts ContactModel = new Contacts();
ContactModel.Phone = "XX-1111111-33";
ContactModel.Fax = "XX-1-1111111";
ContactModel.SerialNo = 4;
ContactModel.IsDefault = true;
ContactModel.AddressID = CurAddressID;
db.Contacts.Add(ContactModel);

db.SaveChanges();
}

Answer

You can do a two step db.SaveChanges();

like this:

Addresses oAdrModel = new Addresses();
oAdrModel.Address1 = "test xxx";
oAdrModel.Address2 = "test xxx";
oAdrModel.SerialNo = 3;
oAdrModel.IsDefault = true;
oAdrModel.CustomerID = 5;
db.Addresses.Add(oAdrModel);
int xx = oAdrModel.AddressID;
db.Addresses.Add(oAdrModel);

db.SaveChanges();

Contacts ContactModel = new Contacts();
ContactModel.Phone = "XX-1111111-33";
ContactModel.Fax = "XX-1-1111111";
ContactModel.SerialNo = 4;
ContactModel.IsDefault = true;
ContactModel.AddressID = oAdrModel.AddressID;
db.Contacts.Add(ContactModel);

db.SaveChanges();