我的实体关系是客户>地址>联系方式。我正在尝试更新客户表中的数据并从Address&中删除特定数据。联系人表格并尝试在地址和地址中再次添加数据联系表。
客户ID是地址表中的FK,地址ID是联系人表格中的FK。
因此,当我在联系表中插入数据时,我必须传递联系表的AddressID值,但我怎么知道当前的AddressID是什么,因为我删除了一个地址数据并再次插入其ID为联系表要插入的ID。我想在一个SaveChanges()
我真的遇到了问题而且不明白如何解决它。这是我的代码。
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,其值不知道导致问题。
我能想到的一种方法是我可以将数据插入到地址表中,然后我调用SaveChanges()
然后我可以获得地址ID ,我可以稍后将其插入到联系表中。我正在寻找最好的建议和指导方针来处理这个问题。感谢
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();
}
查看我的完整代码,我收到错误对象引用未设置
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();
}
EF的POCO课程
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; }
}
此行oAdrModel.Contacts.Add(ContactModel);
投掷错误未将对象引用设置为对象实例
我的代码有什么问题?请帮我解决。
答案 0 :(得分:3)
不是设置父Id
属性并将新实体直接添加到相应的DbSet
,而只需将它们添加到相应的父集合中:
Addresses oAdrModel = new Addresses() { Contacts = new List<Contacts>() };
oAdrModel.Address1 = "test xxx";
oAdrModel.Address2 = "test xxx";
oAdrModel.SerialNo = 3;
oAdrModel.IsDefault = true;
existingCustomer.Addresses.Add(oAdrModel);
Contacts ContactModel = new Contacts();
ContactModel.Phone = "XX-1111111-33";
ContactModel.Fax = "XX-1-1111111";
ContactModel.SerialNo = 4;
ContactModel.IsDefault = true;
oAdrModel.Contacts.Add(ContactModel);
答案 1 :(得分:0)
你可以做两步db.SaveChanges();
像这样: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();
答案 2 :(得分:-1)
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;
ContactModel.Address = oAdrModel
db.Contacts.Add(ContactModel);
db.SaveChanges();
Where Address应该是虚拟地址,并且已加入AddressID:
public class ContacModel{
{
public int AddressID { get; set; }
[ForeignKey("AddressID")]
public virtual Addresses Address { get; set; }
}