How do I fix `System.MissingMethodException: Constructor on type Microsoft.EntityFrameworkCore.DbSet`1 not found.`?

456 views Asked by At

Database Table Export to CSV using EntityFramework DbContext .NET 6

I have been tasked to create a Blazor ASP.NET Core application that will export each table from an MS SQL Server database into it's own .csv file. I have extracted the DbSets from the DbContext properties into a generic list, but when I attempt to cast the generic objects to their DbSet class I get the following error:

An unhandled exception occurred while processing the request. MissingMethodException: Constructor on type 'Microsoft.EntityFrameworkCore.DbSet`1[[DatabaseTableExport.Data.LoginDbModels.AccountPasswordHistory, DatabaseTableExport, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' not found. System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture)

How do I fix this error, or is there a better way to be extracting the DbSets from the DbContext?

using DatabaseTableExport.Data;
using DatabaseTableExport.Data.LoginDbModels;
using Microsoft.AspNetCore.Components;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace DatabaseTableExport.Pages
{
    public partial class Index : ComponentBase
    {
        [Inject]
        private LoginDbContext LoginDbContext { get; set; }
        [Inject]
        private ApplicationDbContext ApplicationDbContext { get; set; }

        protected override void OnInitialized()
        {
            List<DbSet<object>> dbSets = GetDbSets(LoginDbContext);

            // iterate through each DbSet instance
            foreach (var dbSet in dbSets)
            {
                // export to csv here
            }

        }

        private static List<DbSet<object>> GetDbSets(DbContext loginDbContext)
        {
            // Retrieve all the DbSet properties declared on the context class.
            var dbSetProperties = loginDbContext.GetType()
                .GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
                .Where(p => p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(DbSet<>))
                .ToList();

            // Iterate over each DbSet property and add it to the list of dbsets.
            var dbSets = new List<DbSet<object>>();
            foreach (var property in dbSetProperties)
            {
                // If the type of the DbSet is null, skip it.
                if (property.PropertyType.GenericTypeArguments.Length <= 0)
                {
                    continue;
                }

                // Get the generic type arguments and create a corresponding DbSet instance.
                var dbsetGenericType = property.PropertyType.GenericTypeArguments[0];
                var convertedDbSet = Activator.CreateInstance(typeof(DbSet<>).MakeGenericType(dbsetGenericType), property.GetValue(loginDbContext));

                dbSets.Add((DbSet<object>)convertedDbSet);
            }

            // Return the list of DbSet objects.
            return dbSets;
        }
    }
}
1

There are 1 answers

14
Svyatoslav Danyliv On BEST ANSWER

I would suggest different approach. EF Core works without DbContext properties. Metadata information can be retrieved from DbContext's Model and Export can be initiated via CallBack.

Callback interface:

public interface IRecordsetProcessor
{
    void ProcessRecordset<T>(DbContext context, IQueryable<T> recordset, IEntityType entityType)
        where T: class;
}

Recordset enumeration Implemantation:

public static class DbContextUtils
{
    public static void ProcessRecordset<T>(DbContext context, IEntityType entityType, IRecordsetProcessor processor) 
        where T : class
    {
        processor.ProcessRecordset(context, context.Set<T>(), entityType);
    }

    private static MethodInfo _processMethod = typeof(DbContextUtils).GetMethod(nameof(ProcessRecordset))!;

    public static void ProcessRecordsets(DbContext context, IRecordsetProcessor processor)
    {
        var entityTypes = context.Model.GetEntityTypes()
            .Where(et => !et.IsOwned())
            .ToList();

        foreach (var et in entityTypes)
        {
            _processMethod.MakeGenericMethod(et.ClrType).Invoke(null, new object[] { context, et, processor });
        }
    }
}

Simple sample implementation of export:

public class ExportProcessor : IRecordsetProcessor
{
    private const string ColumnSeparator = ",";

    static string PreparePropertyValue(IProperty prop, object record)
    {
        var clrValue = prop.GetGetter().GetClrValue(record);

        // without converter
        var value = clrValue;

        // with converter
        /*
        var converter = prop.GetValueConverter();
        var value = converter == null ? clrValue : converter.ConvertToProvider(clrValue);
        */
        
        if (value == null)
            return "";
        var strValue = value.ToString() ?? "";

        if (strValue.Contains(ColumnSeparator) || strValue.Contains('\"'))
        {
            strValue = "\"" + strValue.Replace("\"", "\"\"") + "\"";
        }

        return strValue;
    }

    public void ProcessRecordset<T>(DbContext context, IQueryable<T> recordset, IEntityType entityType) where T : class
    {
        var exportItems = new List<string>();
        var properties = entityType.GetProperties().ToList();

        // produce header
        exportItems.Add(string.Join(ColumnSeparator, properties.Select(p => p.GetColumnName())));

        foreach (var record in recordset.AsNoTracking())
        {
            exportItems.Add(string.Join(ColumnSeparator, properties.Select(p => PreparePropertyValue(p, record))));
        }

        // TODO: save to file, or do that without intermediate list
    }
}

Usage sample:

DbContextUtils.ProcessRecordsets(context, new ExportProcessor());