通过 linq to sql 更新行时静默失败
我有一些类似于以下示例的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里有几种可能性:
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, yourlistOfCars
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.如果将调用移至循环内的
SubmitChanges
会怎样?What if you move the call to
SubmitChanges
inside your loop?您错过了实际更新模型的调用。我相信你的例子应该是这样的:
另外,更新永远不会默默地失败。如果更新失败,则会抛出 UpdateException。在您的情况下发生的情况是您的代码将要更新,但模型中没有接受任何更新,因此不会更新任何内容。如果您遇到主键或外键约束的问题,您将得到一个例外。
You're missing a call to actually update the models. I believe your example should look like this:
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.