有没有一种方法可以忽略asp.net核心Include函数中的特定属性从多对多关系



我正在开发Asp.net core 5 Web api,我使用用户实体和权限实体之间的多对多关系,我需要使用热切加载从数据库加载用户权限,但数据返回一个循环信息,包括用户的完整信息,但我只加载权限。我正在使用Dto返回Json数据。这是我的型号和代码

权限模型

public class Permission
{
public int Id { get; set; }
public string ClaimName { get; set; }
public IList<UserPermission> UserPermissions { get; set; }
}

用户权限模型

public class UserPermission
{
public int UserId { get; set; }
public AppUser AppUser { get; set; }
public int PermissionId { get; set; }
public Permission Permission { get; set; }
}

用户模型

public class AppUser
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public ICollection<Photo> Photos { get; set; }
public DateTime Created { get; set; } = DateTime.Now;
public DateTime LastActive { get; set; }
public IList<UserPermission> UserPermissions { get; set; }
public bool Status { get; set; }
}

我的用户Dto

public class UserDto
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public ICollection<Photo> Photos { get; set; }
public ICollection<UserPermission> UserPermissions { get; set; }
public string Token { get; set; }
}

我的功能

var sss = _context.UserPermissions
.Include(l => l.Permission)
.Where(v => v.UserId == user.Id)
.ToList();

对要排除的属性使用[JsonIgnore]。注意,我们应该使用";使用Newtonsoft.Json";名称空间不使用";System.Text.Json.Serialization";

最新更新