C# 实体框架6不存储子对象
我向类中添加了一个道具,并添加了一个新的C# 实体框架6不存储子对象,c#,entity-framework,C#,Entity Framework,我向类中添加了一个道具,并添加了一个新的DbSet,但在保存时,它不会存储我的子对象。 我家的班级: [Table("Houses")] public class House { [Key] public int ID { get; set; } public List<Price> Prices { get; set; } // <-- new prop } 我确实在我的Price类中添加了publicvirtualhouse{get;set;}。但
DbSet
,但在保存时,它不会存储我的子对象。
我家的班级:
[Table("Houses")]
public class House
{
[Key]
public int ID { get; set; }
public List<Price> Prices { get; set; } // <-- new prop
}
我确实在我的Price
类中添加了publicvirtualhouse{get;set;}
。但是我没有填充它,因为在填充house对象时,我还不知道db中的ID。也许这就是原因?我还阅读并将其添加到我的Price
课程中:
[ForeignKey("HouseId")]
public virtual House House { get; set; }
public int HouseId { get; set; }
但价格表中仍然没有条目我可能在存储/更新数据库时出错。
编辑当前存储方法:
using (var db = new MyCommon.HouseContext())
{
var l = db.Countries.First(tmpC => tmpC.Code.Equals(h.Country.Code));
h.OperatorId = op.ID;
h.CountryId = l.ID;
h.Country = l;
var existingHouse = db.Houses.Where(p => p.Code.Equals(h.Code, StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault();
if (existingHouse != null)
{
// update
h.ID = existingHouse.ID; // workaround for The property 'ID' is part of the object's key information and cannot be modified.
h.CountryId = existingHouse.CountryId;
h.OperatorId = existingHouse.OperatorId;
db.Entry(existingHouse).CurrentValues.SetValues(h);
db.Entry(existingHouse).State = System.Data.Entity.EntityState.Modified;
//db.SaveChanges(); // moved to bottom for perf.
}
else
{
existingHouse = h;
db.Houses.Add(h); // insert
}
foreach (var ft in existingHouse.Prices)
{
var existingFt = existingHouse.Prices.SingleOrDefault(f => f.ID == ft.ID);
if (existingFt != null)
{
db.Entry(existingFt).CurrentValues.SetValues(ft);
db.Entry(existingFt).State = System.Data.Entity.EntityState.Modified;
}
else
{
existingHouse.Prices.Add(ft);
}
}
db.SaveChanges();
}
检查对象的
EntityState
。将对象附加到您的上下文中,并遍历这些价格
,以标记EntityState.Modified
。如果Price
对象是新对象,您可以使用EntityState.Added。
您可以查看“upsert”模式示例。您正在更新房屋对象,但在什么时候对需要更新的价格进行任何更改?它应该是自动处理的,但很难说,因为您忽略了该部分。确实,但它在if
之前。我在那里填写价格
。调试器显示h.Prices中有100多个对象。Db表保持为空。您是否标记了它们的实体状态。是否修改了(或添加了)?我对“价格”和“功能”之间的区别有点困惑。您的房屋对象有一个列表,但当您循环浏览这些列表时,您会将它们添加到existingHouse.Features
?抱歉,粘贴不正确。这是价格
。但仍然不存储子对象。我正在研究并将其与您的答案结合起来。MSDN链接非常有用。如果修好了会让你知道的!谢谢我已经阅读了msdn的文章和我在之前的评论中发布的SO问题。再加上我认为我编辑的问题应该行得通。因为房屋对象被跟踪,因此也应该跟踪和插入/更新孩子。但数据库中未触及或插入任何行。我错过了一些东西。。。
using (var db = new MyCommon.HouseContext())
{
var l = db.Countries.First(tmpC => tmpC.Code.Equals(h.Country.Code));
h.OperatorId = op.ID;
h.CountryId = l.ID;
h.Country = l;
var existingHouse = db.Houses.Where(p => p.Code.Equals(h.Code, StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault();
if (existingHouse != null)
{
// update
h.ID = existingHouse.ID; // workaround for The property 'ID' is part of the object's key information and cannot be modified.
h.CountryId = existingHouse.CountryId;
h.OperatorId = existingHouse.OperatorId;
db.Entry(existingHouse).CurrentValues.SetValues(h);
db.Entry(existingHouse).State = System.Data.Entity.EntityState.Modified;
//db.SaveChanges(); // moved to bottom for perf.
}
else
{
existingHouse = h;
db.Houses.Add(h); // insert
}
foreach (var ft in existingHouse.Prices)
{
var existingFt = existingHouse.Prices.SingleOrDefault(f => f.ID == ft.ID);
if (existingFt != null)
{
db.Entry(existingFt).CurrentValues.SetValues(ft);
db.Entry(existingFt).State = System.Data.Entity.EntityState.Modified;
}
else
{
existingHouse.Prices.Add(ft);
}
}
db.SaveChanges();
}