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

标签 c# asp.net asp.net-mvc entity-framework-6 signalr

如何允许 SignalR 使用 Entity Framework 6 将更新从 SQL Server 数据库推送到浏览器?

这是我的操作方法:

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);
}

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

ASP.NET MVC 5 SignalR, SqlDependency and EntityFramework 6

我已经尝试了建议的方法,但没有用。除此之外,我还发现了一个非常重要的安全问题,即在隐藏字段中存储敏感数据!

我的问题是:如何根据事务表上的任何 Insert 更新我的 View ?

最佳答案

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

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();
         }
    }

关于c# - 如何允许 SignalR 使用 EF6 将更新从数据库推送到浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58581028/

相关文章:

ASP.NET 错误 : More than one DbContext was found. 指定要使用哪一个

c# - 如何缩短类型名称?

javascript - 错误 : Could not complete the operation due to error 8150002e

C# ASP.NET MVC 3 SQL 数据库到 html.dropdownlistfor 和 AutoPostBack 到选择和项目的浏览操作

asp.net - ASP :menu : need to show sub-menu items when page is loaded with different style

c# - AnonymousType 无法序列化。考虑用 DataContractAttribute 属性标记它

c# - 使用存储库和映射、C# 实现 MVC 设计模式

C#、WPF、Visual Studio 4 : irregular shape color filling

asp.net - 如何在 asp.net mvc 中的 url 中添加页面标题?

c# - 在 C# 中使用 EHLAPI32 和 RUMBA 作为仿真供应商