DbContextOptionsBuilder<someType> 不包含 UseSqlServer() 的定义

DbContextOptionsBuilder<someType> does not contain a definition for UseSqlServer()

我在这里查看了几个类似的问题,但到目前为止没有任何帮助。

我正在尝试基于直到运行时才知道的类型动态创建 DbContext。 DbContext 在我的应用程序中引用的另一个库中。我需要将 DbContextOptions object 传递给 DbContext 的构造函数。所以我正在创建一个 DbContextOptionsBuilder 并尝试调用传入连接字符串的 UseSqlServer() 方法。但是我得到了标题中的错误。

其他类似的问题总是说添加包 Microsoft.EntityFrameworkCore 和 Microsoft.EntityFrameworkCore。SqlServer 和我已经这样做了,但没有成功。

这是我现在的代码:

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.SqlServer;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

private DbContext GetDbContext()
{
    Console.WriteLine("\nGetting DB Context...");

    Assembly LaytonDBSets = Assembly.Load("LaytonDBSets.Core");

    List<TypeInfo> dbContexts = LaytonDBSets.DefinedTypes.Where(t => typeof(DbContext).IsAssignableFrom(t)).ToList();

    foreach (TypeInfo ti in dbContexts)
    {
        Type ctxType = LaytonDBSets.GetType(ti.FullName);

        // Get the DbContextOptions to pass to the DbContext constructor
        Type dbContextOptionsBuilderBase = typeof(DbContextOptionsBuilder<>);
        Type dbContextOptionsBuilderType = dbContextOptionsBuilderBase.MakeGenericType(ctxType);
        dynamic dbContextOptionsBuilder = Activator.CreateInstance(dbContextOptionsBuilderType);
        string connStr = iconfig.GetConnectionString(ti.Name);
        dbContextOptionsBuilder.UseSqlServer(connStr); // ERROR HERE
        var options = dbContextOptionsBuilder.Options;

        dynamic ctx = Activator.CreateInstance(ctxType, args: new object[] { options });

        // stuff to be added...
    }

    // stuff to be added...
}

如果有更好的方法来做我正在尝试的事情,请告诉我,我以前从未做过这样的事情。

这是帮手 class 做的:

public class ContextHelper
{
    public static DbContext CreateDbContext<T>(string connectionString) where T : DbContext
    {
        var optionsBuilder = new DbContextOptionsBuilder<T>();
        optionsBuilder.UseSqlServer(connectionString);

        var options = optionsBuilder.Options;
        var context = (DbContext)Activator.CreateInstance(typeof(T), options);

        return context;
    }

    private static MethodInfo _createDbContext = typeof(ContextHelper).GetMethod("CreateDbContext", new Type[]{typeof(string)});

    public static List<DbContext> CreateDbContexts(IEnumerable<(Type Type, string ConnectionString)> contextTypes)
    {
        var result = new List<DbContext>();
        foreach (var contextType in contextTypes)
        {
            result.Add((DbContext)_createDbContext.MakeGenericMethod(contextType.Type)
                .Invoke(null, new[] {contextType.ConnectionString}));
        }

        return result;
    }
}

您的情况下的用法:

var typeWithConnectionString = 
    dbContexts.Select(ti => (LaytonDBSets.GetType(ti.FullName), iconfig.GetConnectionString(ti.Name)));

var contexts = ContextHelper.CreateDbContexts(typeWithConnectionString);