//EDMX文件http://pastebin.com/btTCRMf7
我有两张桌子Customers和Sites
//Site
public int ID { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public int CustomerID { get; set; }
public int CityID { get; set; }
public int CountryID { get; set; }
public int EncodedBy { get; set; }
public System.DateTime DateEncoded { get; set; }
public virtual City City { get; set; }
public virtual Country Country { get; set; }
public virtual ICollection<Invoice> Invoices { get; set; }
public virtual User User { get; set; }
public virtual Customer Customer { get; set; }
//Customer
public int ID { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public int CityID { get; set; }
public int CountryID { get; set; }
public int CreditTermID { get; set; }
public int EncodedBy { get; set; }
public System.DateTime DateEncoded { get; set; }
public virtual City City { get; set; }
public virtual Country Country { get; set; }
public virtual CreditTerm CreditTerm { get; set; }
public virtual User User { get; set; }
public virtual ICollection<Invoice> Invoices { get; set; }
public virtual ICollection<Site> Sites { get; set; }
//Country
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<Customer> Customers { get; set; }
public virtual ICollection<Site> Sites { get; set; }
//City
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<Customer> Customers { get; set; }
public virtual ICollection<Site> Sites { get; set; }
//SiteModel
private static IQueryable<Site> Build(this DbSet<Site> query)
{
return query.Include("User").Include("City").Include("Country").Include("Customer");
}
public static Site Find(int siteID)
{
using (DragonRentalsEntities context = new DragonRentalsEntities(new ConfigurationManager().ConnectionString))
{
Site result = context.Sites.Build().SingleOrDefault(s => s.ID == siteID);
return result;
}
}
public static Site Update(Site _updatedSite)
{
using (DragonRentalsEntities context = new DragonRentalsEntities(new ConfigurationManager().ConnectionString))
{
context.Sites.Attach(_updatedSite);
context.Entry(_updatedSite).State = EntityState.Modified;
context.SaveChanges();
return Find(_updatedSite.ID);
}
}
Site test = SiteModel.Find(1);
test.City = null;
test.CityID = 1;
test.Country = null;
test.CountryID = 1;
test.Customer = null;
test.CustomerID = 1;
SiteModel.Update(test);我要去找A referential integrity constraint violation occurred: The property values that define the referential constraints are not consistent between principal and dependent objects in the relationship.
但是,可以在更新对象之前添加test.Customer.City = null;。看起来Customer.City和Site.City是矛盾的。有人能解释一下原因吗?或任何解决方法?
发布于 2013-10-08 14:42:46
我可以解释为什么。包括持久化实体以加载所有包含objects.So我们的站点对象具有对您的clusses (城市、国家、用户客户)的所有引用。我认为这就是问题所在。解决方法可以是仅加载site对象:
Site result = context.Sites.SingleOrDefault(s => s.ID == siteID);因此,它将只加载site对象的ids。然后你可以在你需要的时候在运行时通过它的ids加载引用的对象。
我认为这是因为当您使用include时,您操作的是对象和对象集合dbContext会跟踪这些更改并在您调用
context.SaveChanges();稍微重构一下代码btw:
public static Site Update(Site _updatedSite)
{
using (DragonRentalsEntities context = new DragonRentalsEntities(new ConfigurationManager().ConnectionString))
{
if (context.Entry(_updatedSite).State == EntityState.Detached)
context.Entry(_updatedSite).State = EntityState.Modified;// attaches entity and marks it as modified if it is detached
context.SaveChanges();
return _updatedSite; //after save changes u have the same object as u send in your Update function
}
}Comment answer Slauma如果我不将其设置为null,我将无法在update方法中附加它们,同样的错误将被触发
回答:,因为当您包含所有实体时,您已经附加到您的上下文对象。顺便说一句,Include transforms sql inner join语句,因此您的数据库对象快照可能不包含具有该ID的City。
https://stackoverflow.com/questions/19074978
复制相似问题