Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在使用ms sql server数据库的asp.net c#mvc codefirst方法应用程序的数据库表中添加或更新记录时,如何获得通知?_C#_Asp.net Mvc_Ef Code First - Fatal编程技术网

在使用ms sql server数据库的asp.net c#mvc codefirst方法应用程序的数据库表中添加或更新记录时,如何获得通知?

在使用ms sql server数据库的asp.net c#mvc codefirst方法应用程序的数据库表中添加或更新记录时,如何获得通知?,c#,asp.net-mvc,ef-code-first,C#,Asp.net Mvc,Ef Code First,我使用codefirst方法在asp.net MVC中开发了一个基本应用程序,其基本模型如下: { public int Id { get; set; } public string title { get; set; } public string location { get; set; } public DateTime Expected_Date_of_Start { get; set; } public DateTime Expected_Da

我使用codefirst方法在asp.net MVC中开发了一个基本应用程序,其基本模型如下:

 {
    public int Id { get; set; }
    public string title { get; set; }
    public string location { get; set; }
    public DateTime Expected_Date_of_Start { get; set; }
    public DateTime Expected_Date_of_End { get; set; }
 }
我可以插入、更新、删除实体,所有功能都正常工作。 现在我想在有人插入新记录或更新现有记录时得到通知

为了澄清这一点,我想以某种方式构建一个通知系统,就像facebook或twitter在发生任何变化时通知用户的方式一样


如何使用asp.net mvc codefirst方法和c语言实现这样的事情?

首先,您需要捕获表修改事件。正如您所写的EF CodeFirst一样,我建议您覆盖SaveChanges。大概是这样的:

Public Overrides Function SaveChanges() As Integer
   For Each changedEntity In Me.ChangeTracker.Entries
       Select Case If(changedEntity.Entity.GetType.Namespace = "System.Data.Entity.DynamicProxies", changedEntity.Entity.GetType.BaseType, changedEntity.Entity.GetType)
           Case GetType(YourEntityType)
               If changedEntity.State = EntityState.Added OrElse changedEntity.State = EntityState.Modified OrElse changedEntity.State = EntityState.Deleted Then
                  NotifyUser(changedEntity)
               End If
       End Select
   Next 
End Function

然后,您需要将此事件通知用户(NotifyUser函数)。为此,您可以使用SignalR或@Elyas Esna和@Girish所写的其他东西。

您可以使用SignalR(带或不带sqlEventHandlerAny参考)了解如何将SignalR与codefirst集成?使用codefirst并不重要,您只需在每次插入、更新或删除时调用signalr函数,或者您可以使用SQL的
ServiceBroker
功能,只需搜索它即可找到实现方式和示例您可以按照其他建议使用signalr。另一种选择是使用azure的通知中心(如果您托管在云上并使用azure服务)。您还可以查看Kafka(通常在大型级别上使用,并支持许多其他东西),具有Azure功能的Azure服务总线可能是。