8

I got a method that receives an entity like string to return a list of that entity.

What I want is set the entity dynamically and return the result.

What I need is something like this:

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

This the error that I getting:

Error

Maybe this is not the correct to do so.

The Set method comes from Entity Framework Core.

Set Method

Timothy Macharia
  • 2,641
  • 1
  • 20
  • 27
Rabel Obispo
  • 529
  • 1
  • 6
  • 14

3 Answers3

1

1st - this is an example, not a solution for your specific problem.

My entity class

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; }

    }
}

My Context

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());
           
        }
    }
}

Example Solution
I have 3 approachs

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();
}


the main program calls.

  1. Yo need the context, AssemblyQualifiedName = parameters. The type name of result = type parameters.

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

  1. Yo need the context = parameters. The type name of result = type parameters.

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

  1. Yo need the context = parameters. The type name of result = type 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();
Joma
  • 3,520
  • 1
  • 29
  • 32
0

You Can Use Generic type Like Below:

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

but For this Your Method Should Works With Generics Like Example at below (This Code Not Tested):

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

You Can Learn more About it in This Link

Sorry For Bad English. I Wish its Helps ...

Ramin Azali
  • 198
  • 11
0

Create a dummy instance to spoof the binding of a generic method

public IEnumerable<T> GetEntity<T>() where T : class
{
    Type type = typeof(T);
    Type spoofedType = typeof(InternalDbSet<>).MakeGenericType(type);
    //instance is only used to spoof the binding
    dynamic instance = Activator.CreateInstance(spoofedType, context, spoofedType.Name);
    return SpoofedMethod(instance);
}

public IEnumerable<T> SpoofedMethod<T>(DbSet<T> _) where T : class
{
     return context.Set<T>().Select(e => e);
}

Activator.CreateInstance takes the type and passes the remaining parameters into the Type's constructor. InternalDbSet<> takes 2 parameters: DbContext and EntityTypeName. DbSet itself is an abstract, so you can't create an instance of it. If you check the type of a DbSet at runtime, then you'll see it is InternalDbSet.

skenneth
  • 1
  • 1