EF Code First:在哪里可以找到 SavingChanges 事件?

发布于 2024-12-11 03:53:17 字数 395 浏览 0 评论 0原文

好的,这可能是一个新手问题,但是我如何/在哪里可以订阅 ObjectContext.SavingChanges 事件,例如在此 帖子

我的演示应用程序中只有两个相关类:“Country”类和一个包含 EF Code First“定义”的类:

internal class TestDb : DbContext
{
    public DbSet<Country> Countries { get; set; }       
}

非常感谢任何提示。

OK, this may be a newbie question, but how/where can I subscribe to the ObjectContext.SavingChanges event as mentioned for example in this post.

I've only two relevant classes in my demo app: The "Country" class and a class which holds the EF Code First "definitions":

internal class TestDb : DbContext
{
    public DbSet<Country> Countries { get; set; }       
}

Any hint is highly appreciated.

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

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

发布评论

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

评论(3

从此见与不见 2024-12-18 03:53:17

您应该能够执行以下操作:

internal class TestDb : DbContext  
{  
    public void SetSavingChanges(EventHandler evt) 
    {
            var oc = this as IObjectContextAdapter;
            oc.ObjectContext.SavingChanges -= evt;
            oc.ObjectContext.SavingChanges += evt;
    }

    public DbSet<Country> Countries { get; set; }  
}  

You should be able to do this:

internal class TestDb : DbContext  
{  
    public void SetSavingChanges(EventHandler evt) 
    {
            var oc = this as IObjectContextAdapter;
            oc.ObjectContext.SavingChanges -= evt;
            oc.ObjectContext.SavingChanges += evt;
    }

    public DbSet<Country> Countries { get; set; }  
}  
东京女 2024-12-18 03:53:17

从我在 MSDN 上看到的内容来看,DbContextObjectContext 的实例包装为外观,而不公开此特定事件。

但是,DbContext确实有一个采用 ObjectContext 的构造函数重载 - 您可以使用它来传入 ObjectContext 并订阅 SavingChanges其上的事件。

using(ObjectContext context = new ObjectContext(myConnectionString))
{
  using(DbContext dbContext = new DbContext(context, true))
  {

  }
}

From what I can see on MSDN, DbContext wraps an instance of ObjectContext as a facade, not exposing this specific event.

However, DbContext does have a constructor overload that takes an ObjectContext - you can use this to pass in an ObjectContext and subscribe to the SavingChanges event on it.

using(ObjectContext context = new ObjectContext(myConnectionString))
{
  using(DbContext dbContext = new DbContext(context, true))
  {

  }
}
书信已泛黄 2024-12-18 03:53:17

另一种方式可能是,例如:

Public Class MainForm

  Private WithEvents myObjectContext As ObjectContext
  Private myDbContext As DbContext

...

  Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles Me.Load
    myDbContext = New DbContext
    myObjectContext = CType(myDbContext, IObjectContextAdapter).ObjectContext

...


  Private Sub ObjectContextSavingChanges(sender As Object, e As EventArgs) Handles myObjectContext.SavingChanges

    'Your code hear

  End Sub

Another way may be, for example:

Public Class MainForm

  Private WithEvents myObjectContext As ObjectContext
  Private myDbContext As DbContext

...

  Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles Me.Load
    myDbContext = New DbContext
    myObjectContext = CType(myDbContext, IObjectContextAdapter).ObjectContext

...


  Private Sub ObjectContextSavingChanges(sender As Object, e As EventArgs) Handles myObjectContext.SavingChanges

    'Your code hear

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