gpt4 book ai didi

c# - 外键冲突

转载 作者:行者123 更新时间:2023-11-30 12:29:14 27 4
gpt4 key购买 nike

//EDMX文件 http://pastebin.com/btTCRMf7

我有 2 个表 CustomersSites

//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);

我得到 发生引用完整性约束冲突:定义引用约束的属性值在关系中的主体和依赖对象之间不一致。

但是,在更新对象之前添加 test.Customer.City = null; 是可行的。好像 Customer.City 和 Site.City 是冲突的。有人可以解释为什么吗?或任何解决方法?

最佳答案

我可以解释为什么。包含持久化实体以加载所有包含对象。因此我们的站点对象具有对您的分类(城市、国家、用户客户)的所有引用。我认为这就是问题所在。解决办法是只加载站点对象:

Site result = context.Sites.SingleOrDefault(s => s.ID == siteID);

所以它只会加载站点对象的 ID。比起您可以在需要的运行时通过它的 ID 加载引用的对象。

我想是因为当你使用include的时候,你操作的是对象和对象的集合,dbContext会跟踪这个变化,并在你调用的时候保存起来

context.SaveChanges();

一点重构代码顺便说一句:

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
}
}

评论回答Slauma 如果我不将它设置为 null,我将无法在更新方法中附加它们,将触发相同的错误

回答:因为当您包含所有实体时,您已经附加到您的上下文对象。顺便说一句,包括对 sql 内部连接语句的转换,因此您的数据库对象快照可能不包含具有该 ID 的城市。

关于c# - 外键冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19074978/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com