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();
}
Current Code
See my full code and i am getting error Object reference not set
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 = "New Customer";
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 = "New test xxx";
oAdrModel.Address2 = "New test xxx";
oAdrModel.SerialNo = 3;
oAdrModel.IsDefault = true;
//oAdrModel.CustomerID = 5;
existingCustomer.Addresses.Add(oAdrModel);
//db.Addresses.Add(oAdrModel);
//db.SaveChanges();
//int CurAddressID = oAdrModel.AddressID;
Contacts ContactModel = new Contacts();
ContactModel.Phone = "New XX-1111111-33";
ContactModel.Fax = "New XX-1-1111111";
ContactModel.SerialNo = 4;
ContactModel.IsDefault = true;
oAdrModel.Contacts.Add(ContactModel);
//ContactModel.AddressID = CurAddressID;
//db.Contacts.Add(ContactModel);
db.SaveChanges();
}
POCO classes for EF
public class CustomerBase
{
public int CustomerID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
[NotMapped]
public string Address1 { get; set; }
[NotMapped]
public string Address2 { get; set; }
[NotMapped]
public string Phone { get; set; }
[NotMapped]
public string Fax { get; set; }
}
public class Customer : CustomerBase
{
public virtual List<Addresses> Addresses { get; set; }
}
public class Addresses
{
[Key]
public int AddressID { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public bool IsDefault { get; set; }
public int SerialNo { get; set; }
public virtual List<Contacts> Contacts { get; set; }
public int CustomerID { get; set; }
//[ForeignKey("CustomerID")]
public virtual Customer Customer { get; set; }
}
public class Contacts
{
[Key]
public int ContactID { get; set; }
public string Phone { get; set; }
public string Fax { get; set; }
public bool IsDefault { get; set; }
public int SerialNo { get; set; }
public int AddressID { get; set; }
//[ForeignKey("AddressID")]
public virtual Addresses Customer { get; set; }
}
this line oAdrModel.Contacts.Add(ContactModel);
throwing error Object reference not set to an instance of an object
what is wrong in my code ? please help me to fix it.