通过 linq to sql 更新行时静默失败

发布于 2024-11-03 04:45:04 字数 424 浏览 0 评论 0原文

我有一些类似于以下示例的 C# 代码:

foreach (car c in listOfCars) {
  c.garage.last_opened_at = DateTime.Now;
}
db.SubmitChanges();

基本上,汽车车库 处于一对一的关系(我知道,这是一个有些缺陷的示例),我正在更新每个车库上次开放的时间。

但是,当我检查数据库时,last_opened_at 字段没有更新(没有显示错误消息)。有什么建议吗?我环顾四周和谷歌,我看到的解决方案都提到garage需要有一个主键。但我仔细检查了一下,我的车库表确实使用其关联汽车的外键作为主键。

问题是主键也兼作外键(即我是否需要专用的主键列)?

I have some C# code that looks like the following sample:

foreach (car c in listOfCars) {
  c.garage.last_opened_at = DateTime.Now;
}
db.SubmitChanges();

Basically, cars and garages are in a one-to-one relationship (a somewhat flawed example, I know), and I'm updating the time each garage was last opened.

When I check the database, though, the last_opened_at field isn't being updated (no error messages are being displayed). Any suggestions why? I looked around SO and on Google, and the solutions I saw all mentioned that garage needs to have a primary key. But I double-checked, and my garage table is indeed using its foreign key to its associated car as a primary key.

Is the problem that the primary key is also doubling as a foreign key (i.e., do I need a dedicated primary key column)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

赴月观长安 2024-11-10 04:45:04

这里有几种可能性:last_opened_at 被指定为数据库提供的字段,您的 linq to sql 类上没有列出主键,而您的 listOfCars 则不太可能不是来自同一个数据库上下文。

如果将该字段标记为由数据库生成(这是使用时间戳或具有数据库默认值的字段时的典型情况),则 linq to sql 将永远不会尝试写入该属性。

如果 linq to sql 类上没有主键,则无论数据库在做什么,它都不会写入表。只要主键是主键,主键同时也是外键就不会有问题。我曾经遇到过这个问题,并认为我记得在这种情况下有一个错误,但我不能 100% 确定。

如果您从一个数据上下文生成 listOfCars,并尝试将它们保存在另一个数据上下文中,则第二个数据上下文甚至不知道这些实体存在。您可以通过确保使用相同的数据上下文或将实体附加到新上下文来解决此问题。

There are few possibilities here: last_opened_at is specified as a database supplied field, you do not have a primary key listed on your linq to sql class and less likely, your listOfCars does not come from the same db context.

If you marked the field as generated by database, a typical scenario when using timestamps or fields with database defaults, then linq to sql will never attempt to write that property.

If you do not have a primary key on the linq to sql class, regardless of what the database is doing, it will not write to the table. Having the primary key also be a foreign key should not be a problem as long as it is the primary. I ran into this once and thought I remembered an error in this case, but I'm not 100% sure.

If you generate the listOfCars from one data context, and try to save them on another data context, the second data context has no idea those entities even exist. You can solve this by ensuring you are using the same data context or attaching the entities to the new context.

倒数 2024-11-10 04:45:04

如果将调用移至循环内的 SubmitChanges 会怎样?

foreach (car c in listOfCars) {
  c.garage.last_opened_at = DateTime.Now;
  db.SubmitChanges();
}

What if you move the call to SubmitChanges inside your loop?

foreach (car c in listOfCars) {
  c.garage.last_opened_at = DateTime.Now;
  db.SubmitChanges();
}
夢归不見 2024-11-10 04:45:04

您错过了实际更新模型的调用。我相信你的例子应该是这样的:

foreach(car c in listOfCars) {
    c.garage.last_opened_at = DateTime.Now;
}
db.AcceptAllChanges();
db.SubmitChanges();

另外,更新永远不会默默地失败。如果更新失败,则会抛出 UpdateException。在您的情况下发生的情况是您的代码将要更新,但模型中没有接受任何更新,因此不会更新任何内容。如果您遇到主键或外键约束的问题,您将得到一个例外。

You're missing a call to actually update the models. I believe your example should look like this:

foreach(car c in listOfCars) {
    c.garage.last_opened_at = DateTime.Now;
}
db.AcceptAllChanges();
db.SubmitChanges();

Also, updates will never silently fail. If the update fails, it'll throw an UpdateException. What's happening in your case is that your code is going to update but sees no updates accepted in the models and therefore doesn't update anything. If you had a problem with primary or foreign key constraints, you'd get an exception.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文