簡體   English   中英

如何將動態實體設置為 dbcontext Entity Framework Core?

[英]How to set dynamic entity to dbcontext Entity Framework Core?

我得到了一個方法,它接收一個像 string 這樣的實體來返回該實體的列表。

我想要的是動態設置實體並返回結果。

我需要的是這樣的:

DbContext.Set <Type.GetType("Entity")>().Select(e => e);

這是我得到的錯誤:

錯誤

也許這不是正確的做法。

Set方法來自 Entity Framework Core。

設置方法

您可以使用如下的通用類型

DbContext.Set<T>().Select(e => e);

但為此,您的方法應該適用於下面的示例(此代碼未測試):

public Task<object> MyMethod<T>(T model)
{
   // some Code
}

您可以在此鏈接中了解更多信息

抱歉英語不好。 我希望它的幫助...

第一 - 這是一個例子,不是您特定問題的解決方案。

我的實體類

using System;
using System.Collections.Generic;
using System.Text;

namespace MyIdentityModel
{
    public class IdentityRole
    {
        public long Id { get; set; } 
        public string Name { get; set; } = null!;
        public DateTimeOffset CreatedAt { get; set; }
        public bool Active { get; set; }
        public bool Enabled { get; set; }
        public DateTimeOffset ActiveUntil { get; set; }

    }
}

我的背景

using MyIdentityModel.Configuration;
using Microsoft.EntityFrameworkCore;
using System;

namespace MyIdentityModel
{

    public abstract class IdentityDbContextBase: DbContext
    {

        public IdentityDbContextBase(DbContextOptions options) : base(options)
        {
        }

        
        public DbSet<IdentityRole> Roles { get; set; } = null!;
        

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
                        modelBuilder.ApplyConfiguration(new IdentityRoleConfiguration());
           
        }
    }
}

示例解決方案
我有 3 種方法

public List<TResult> GetList<TResult>(DbContext context, string entityTypeName)
    where TResult : class
{
    Type entityType = Type.GetType(entityTypeName); // get type of entity / MyIdentityModel.IdentityRole
    var setMethod = context.GetType().GetMethod(nameof(DbContext.Set), new Type[] { }).MakeGenericMethod(entityType); //Get Method Set<MyIdentityModel.IdentityRole>
    var dbSet = setMethod.Invoke(context, new object[] { }); // Calling context.Set<MyIdentityModel.IdentityRole>
    /* Call to another methods using reflection before calling "ToList" 
        Eg. example Where, Include, Select
        */
    var toListMethod = typeof(Enumerable).GetMethod("ToList").MakeGenericMethod(entityType); //get Extension Method "ToList<MyIdentityModel.IdentityRole>"
    var list = (List<TResult>)toListMethod.Invoke(null, new object[] { dbSet });//Calling "context.Set<MyIdentityModel.IdentityRole>.ToList<MyIdentityModel.IdentityRole>()" Method and convert to destination type if is possible.
    return list;
}

public List<TEntity> GetList2<TEntity>(DbContext context)
    where TEntity : class
{
    var setMethod = context.GetType().GetMethod(nameof(DbContext.Set), new Type[] { }).MakeGenericMethod(typeof(TEntity)); //Get Method Set<MyIdentityModel.IdentityRole>
    var dbSet = setMethod.Invoke(context, new object[] { }); // Calling context.Set<MyIdentityModel.IdentityRole>
    /* Call to another methods using reflection before calling "ToList" 
        Eg. example Where, Include, Select
        */
    var toListMethod = typeof(Enumerable).GetMethod("ToList").MakeGenericMethod(typeof(TEntity)); //get Extension Method "ToList<MyIdentityModel.IdentityRole>"
    var list = (List<TEntity>)toListMethod.Invoke(null, new object[] { dbSet });//Calling "context.Set<MyIdentityModel.IdentityRole>.ToList<MyIdentityModel.IdentityRole>()" Method and convert to destination type if is possible.
    return list;
}

public List<TEntity> GetList3<TEntity>(DbContext context)
    where TEntity : class
{
    return context.Set<TEntity>()/* Call to another methods before "ToList" */.ToList();
}


主程序調用。

  1. 你需要上下文, AssemblyQualifiedName = parameters 結果的類型名稱=類型參數

public List<TResult> GetList<TResult>(DbContext context, string entityTypeName)

  1. 你需要 context = parameters 結果的類型名稱=類型參數

public List<TEntity> GetList2<TEntity>(DbContext context)

  1. 你需要 context = parameters 結果的類型名稱=類型參數

public List<TEntity> GetList3<TEntity>(DbContext context)

Console.WriteLine("█ Result 1");
var result = GetList<MyIdentityModel.IdentityRole>(context, typeof(MyIdentityModel.IdentityRole).AssemblyQualifiedName);
Console.WriteLine(JsonSerializer.Serialize(result));
Console.WriteLine();

Console.Write("█ Result 2");
var result2 = GetList2<MyIdentityModel.IdentityRole>(context);
Console.WriteLine(JsonSerializer.Serialize(result2));
Console.WriteLine();

Console.Write("█ Result 3");
var result3 = GetList3<MyIdentityModel.IdentityRole>(context);
Console.WriteLine(JsonSerializer.Serialize(result3));
Console.WriteLine();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM