微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Entity Framework Core 中的属性/列级别访问

如何解决Entity Framework Core 中的属性/列级别访问

this 问题中,我询问了一种在 .NET 5 中实现属性/字段级别访问的方法一个想法是在 EF Core 配置中实现魔法,以便仅从数据库加载这些属性当前用户有权访问。在第二步中,使用认条件的 function AutoComment(comment_char,comment_boolean) range if len(a:comment_char) == 0 let comment_character = GetCommentChar() else let comment_character = a:comment_char endif let vis_length = a:lastline - a:firstline let vis_length = vis_length . 'j' if vis_length == '0j' let vis_length = '' endif " get first character in a line. let move_to_first = "normal! mq^" execute move_to_first let line = getline('.') let first_nonspace = col('.') - 1 let end_char = first_nonspace + strlen(comment_character) - 1 let current_chars = line[first_nonspace : end_char] " let current_char = matchstr(getline('.'),'\%' . col('.') . 'c.') " echo current_char let change_line = "normal! \<C-V>" . vis_length if a:comment_boolean == 'c' let change_line = change_line . "I" . comment_character . " " elseif a:comment_boolean == 'u' if current_chars == comment_character let move_right = strlen(comment_character) let change_line = change_line . move_right . "lx" endif endif " Can add additional line here to highlight the prevIoUs lines " Can't decide a way that it'll work seamlessly,so ignore for Now. let change_line = change_line . "\<esc>`q" " echo change_line execute change_line endfunction 属性将空属性从 dto 中排除。我已尽力使用实体属性上的自定义属性属性在上下文的 JsonIgnore 挂钩中实现逻辑:

OnModelCreating

public class MyEntity : BaseEntity { [IncludeForRoles(RoleNames.Staff)] public string InternalDetails { get; private set; } } public class MyContext : IdentityDbContext<User,Role,Guid>,IMyContext { private readonly ITokenAccessor _tokenAccessor; public MyContext( DbContextOptions options,ITokenAccessor tokenAccessor) : base(options) { _tokenAccessor = tokenAccessor; } [...] protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); builder.ApplyConfigurationsFromAssembly(typeof(UserConfiguration).Assembly); foreach (Type type in MyContextUtility.GetEntityTypes()) { MethodInfo ignoreFlagMethod = MyContextUtility.SetRoleBasedIgnoreFlagMethod.MakeGenericmethod(type); ignoreFlagMethod.Invoke(this,new object[] { builder,_tokenAccessor.UserRoles }); } } } public static class MyContextUtility { private static IList<Type> _entityTypeCache; public static IList<Type> GetEntityTypes() { if (_entityTypeCache != null) { return _entityTypeCache.ToList(); } Assembly assembly = typeof(BaseEntity).Assembly; _entityTypeCache = (from t in assembly.DefinedTypes where t.BaseType == typeof(BaseEntity) select t.AsType()).ToList(); return _entityTypeCache; } public static readonly MethodInfo SetRoleBasedIgnoreFlagMethod = typeof(MyContextUtility).getmethods(BindingFlags.Public | BindingFlags.Static) .Single(t => t.IsGenericmethod && t.Name == nameof(SetRoleBasedIgnoreFlag)); public static void SetRoleBasedIgnoreFlag<T>(ModelBuilder builder,IList<string> userRoles) where T : BaseEntity { IEnumerable<PropertyInfo> props = typeof(T).GetProperties().Where( prop => Attribute.IsDefined(prop,typeof(IncludeForRolesAttribute))); foreach (PropertyInfo prop in props) { IncludeForRolesAttribute attr = prop.GetCustomAttribute<IncludeForRolesAttribute>(); if (!userRoles.Intersect(attr.RoleNames).Any()) { Debug.WriteLine($"Adding ignore flag for type '{typeof(T).Name}' and property {prop.Name}."); builder.Entity<T>().Ignore(prop.Name); } } } } 提供对当前用户的 jwt 访问令牌的访问。 上面的代码不起作用,因为 ITokenAccessor用户登录之前执行,这就是 OnModelCreating 此时始终为空的原因。

有没有办法在实体框架核心中实现基于属性/列的访问?我花了几个小时研究,但不幸的是找不到解决方案。而且我很惊讶基于属性/列的访问应该是一个不寻常的要求。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。