小编Olg*_*ova的帖子

如何使用 AutoDetectChangesEnabled = false 在实体框架中更新多对多

请帮我处理这种情况:

我故意关掉AutoDetectChangesEnabled,我也故意加载我的实体AsNoTracked()

在这种情况下我无法更新多对多关系:

这是更新方法的代码:

public void Update(User user)
{
  var userRoleIds = user.Roles.Select(x => x.Id);
  var updated = _users.Find(user.Id);

  if (updated == null)
  {
    throw new InvalidOperationException("Can't update user that doesn't exists in database");
  }

  updated.Name = user.Name;
  updated.LastName = user.LastName;
  updated.Login = user.Login;
  updated.Password = user.Password;
  updated.State = user.State;

  var newRoles = _roles.Where(r => userRoleIds.Contains(r.Id)).ToList();
  updated.Roles.Clear();
  foreach (var newRole in newRoles)
  {
    updated.Roles.Add(newRole);
  }

  _context.Entry(updated).State = EntityState.Modified;            
}
Run Code Online (Sandbox Code Playgroud)

所有简单的字段,例如NameLastName都已更新。 …

.net c# many-to-many entity-framework

5
推荐指数
0
解决办法
415
查看次数

压缩操作过滤器 ASP.NET Core

我需要使用 ASP.NET Core 创建压缩操作过滤器

我找到了一些 MVC 5 的示例,例如:

public class CompressAttribute : ActionFilterAttribute   {

public override void OnActionExecuting(ActionExecutingContext filterContext)
{

    var encodingsAccepted = filterContext.HttpContext.Request.Headers["Accept-Encoding"];
    if (string.IsNullOrEmpty(encodingsAccepted)) return;

    encodingsAccepted = encodingsAccepted.ToLowerInvariant();
    var response = filterContext.HttpContext.Response;

    if (encodingsAccepted.Contains("deflate"))
    {
        response.AppendHeader("Content-encoding", "deflate");
        response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
    }
    else if (encodingsAccepted.Contains("gzip"))
    {
        response.AppendHeader("Content-encoding", "gzip");
        response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
    }
}  
Run Code Online (Sandbox Code Playgroud)

但是当我尝试使用它时

using Microsoft.AspNet.Http;
Run Code Online (Sandbox Code Playgroud)

它根本不起作用:

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc.Filters;
using Microsoft.Extensions.Primitives; …
Run Code Online (Sandbox Code Playgroud)

c# compression filter asp.net-core

5
推荐指数
1
解决办法
5495
查看次数