如何允许SignalR使用EF6将更新从数据库推送到浏览器

Any*_*are 2 c# asp.net asp.net-mvc signalr entity-framework-6

如何使用允许SignalR将更新从推SQLServer DB送到浏览器EF6

这是我的操作方法:

public ActionResult Index()
        {
            var currentGates = _ctx.Transactions
                                                .GroupBy(item => item.SubGateId)
                                                .SelectMany(group => group.OrderByDescending(x => x.TransactionDateTime)
                                                .Take(1))
                                                .Include(g => g.Card)
                                                .Include(g => g.Student)
                                                .Include(g => g.Student.Faculty)
                                                 .Include(g => g.Student.Department)
                                                .Include(g => g.SubGate)
                                                .ToList();


            return View(currentGates);
        }
Run Code Online (Sandbox Code Playgroud)

经过大量搜索,我得到的唯一结果是:

ASP.NET MVC 5 SignalR,SqlDependency和EntityFramework 6

我尝试了建议的方法,但是它没有用,此外,我发现了一个非常重要的安全问题,涉及在隐藏字段中存储敏感数据!

我的问题是:

如何根据Insert交易表中的任何内容更新我的视图?

Vin*_*nce 5

因此,基本上,您需要做的是覆盖SaveChanges()SignalR函数的方法和操作:

public class ApplicationDbContext : IdentityDbContext<IdentityUser>
  {
  public override int SaveChanges()
        {
            var entities = ChangeTracker.Entries().Where(x => x.Entity is Transactions && x.State == EntityState.Added) ;
            IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
            foreach (var entity in entities)
            {
              hubContext.Clients.All.notifyClients(entity);
            }

            return base.SaveChanges();
         }
    }
Run Code Online (Sandbox Code Playgroud)