Linq to Sql 中的信号?
有谁知道有一种方法可以做类似于 Django 的 signals
使用 LINQ to SQL?
我试图记录何时插入新行以及何时更新某些列,所以我真的只想要 pre_save
和 post_save
信号。
我可以通过使用 OnFooIDChanging()
和 OnFooIDChanged()
等定义的部分来对某些模型执行此操作(其中 FooID
是主键),但这对于主键不是身份或由代码设置的模型不起作用。
对于这些,我可能会使用 OnValidate()
,但这只是 pre_save
,并且它使得处理数据库变得困难,因为 OnValidate()
code> 是从 DBContext.SubmitChanges()
调用的,这当然不允许从内部调用第二个 SubmitChanges()
,从而使得 post_save
代码>据我所知基本上不可能。
Does anyone know of a way to do something similar to Django's signals
using LINQ to SQL?
I'm trying to record when new rows are inserted and when certain columns are updated, so I really just want pre_save
and post_save
signals.
I can kind of do it with some models by using the partials defined like OnFooIDChanging()
and OnFooIDChanged()
(where FooID
is a primary key), but this doesn't work for models whose primary key is not an identity, or is set by code.
For those, I could possibly use OnValidate()
, but that would only be pre_save
, and it makes dealing with the database tough, since OnValidate()
is called from DBContext.SubmitChanges()
, which of course doesn't allow a second SubmitChanges()
to be called from within, making post_save
basically impossible as far as I can see.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我已经彻底陷入了这个问题,但我认为我有一个非常酷的解决方案:
首先,向您的数据上下文添加一个事件处理程序,该处理程序将收集所有保存后信号并隐藏
Dispose
方法,以便我们可以在处理之前调用该事件。 (请注意,我使用new
关键字而不是override
。这使得调用事件成为可能。)接下来,编写一个 T4 模板,用于检查 Linq to Sql 为您生成的
dbml
文件。对于数据库中的每个表(以及每个分部类),使用以下方法添加到分部。
还可以通过 T4 添加另一个
partial MyDataContext
。 这将为 Linq to SQL 为您提供的部分方法添加定义(如 Merritt 提到的)。将这些文件隐藏在安全的地方,这样就没有人试图弄乱它们。
您的信号框架已设置。 现在您可以编写信号了。 将它们放在
Foo.cs
中,或者全部放在Signals.cs
文件中:这有点复杂,所以如果有任何不明白的地方,请发表评论我会尽力解决这个问题。
Ok, I've gone completely down the rabbit hole on this one, but I think I have a pretty cool solution:
First, add an event handler to your data context that will collect all of the post-save signals and hide the
Dispose
method so that we can call the event right before we dispose. (Note that I use thenew
keyword instead ofoverride
. This makes calling the event possible.)Next, write a T4 Template that inspects the
dbml
file that Linq to Sql generates for you.For each table in the database (and thus each partial class), add on to the partial with the following methods.
Also add another
partial MyDataContext
via T4. This will be adding definitions to the partial methods that Linq to SQL gives you (as Merritt mentioned).Hide those files away somewhere safe, so no one tries to mess with them.
Your signals framework is set up. Now you can write your signals. Put these either in
Foo.cs
or all together in aSignals.cs
file:This is a bit complex, so if anything doesn't make sense, please leave a comment and I'll do my best to address it.
我有一个比我已经发布的解决方案更简单的解决方案,无论如何都不起作用:覆盖 SubmitChanges(ConflictMode failureMode):
使用实体框架,我会执行类似于您尝试执行的操作:保存实体后,我插入出于审计目的进入不同表的新条目(它是更改之前实体的副本)。 EF 实体容器(如数据上下文)上有一个 SaveChanges() 事件,允许您在保存更改之前添加到当前上下文。
I have a much easier solution than what I already posted which didn't work anyway: override SubmitChanges(ConflictMode failureMode):
With the Entity Framework, I do something similar to what you are trying to do: after I save a entity, I insert a new entry into a different table for auditing purposes (it's a copy of the entity before the changes). there is a SaveChanges() event on the EF entities container (like the data context) that allows you to add to the current context before changes are saved.