如何允许 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/