C# 实体拦截所有数据库更改

C# 实体拦截所有数据库更改,c#,asp.net,entity-framework,C#,Asp.net,Entity Framework,在现有的实体应用程序中,我需要在每次数据库更改时调用远程API 我想知道我是否能以某种方式钩住实体中的事件或类来编写代码。类似于下面的Psuedo代码 if (Table == "Business") Api.Call 您可以将context.SaveChanges()包装到存储库中的方法调用中,并在其中处理更改 using System.Data.Entity.Infrastructure; //may be wrong syntax public bool SaveAllChanges(

在现有的实体应用程序中,我需要在每次数据库更改时调用远程API

我想知道我是否能以某种方式钩住实体中的事件或类来编写代码。类似于下面的Psuedo代码

if (Table == "Business") Api.Call

您可以将
context.SaveChanges()
包装到存储库中的方法调用中,并在其中处理更改

using System.Data.Entity.Infrastructure;

//may be wrong syntax
public bool SaveAllChanges()
{
   var Changes = context.ChangeTracker.Entries()
                        .Where(e => e.State == EntityState.Modified | e.State == EntityState.Added | e.State ...);
   foreach(var change in Changes)
   {
     //check if you need to call your API
   }

   return context.SaveChanges() > 0 
}

您可以将
context.SaveChanges()
包装到存储库中的方法调用中,并在其中处理更改

using System.Data.Entity.Infrastructure;

//may be wrong syntax
public bool SaveAllChanges()
{
   var Changes = context.ChangeTracker.Entries()
                        .Where(e => e.State == EntityState.Modified | e.State == EntityState.Added | e.State ...);
   foreach(var change in Changes)
   {
     //check if you need to call your API
   }

   return context.SaveChanges() > 0 
}