运行时使用 EF 核心编译 DbContext,缺少对 Migrator() 的引用

Runtime compiling DbContext with EF core, missing reference for Migrator()

我正在构建一个需要在运行时创建 EntityFrameWorkCore DbContext 然后迁移的应用程序,如果我不使用 dbContext.Database.Migrate() 方法,下面的代码会编译并运行,但如果我这样做我收到有关缺少 directive/reference.

的诊断错误

error CS1061: 'DatabaseFacade' does not contain a definition for 'Migrate' and no accessible extension method 'Migrate' accepting a first argument of type 'DatabaseFacade' could be found (are you missing a using directive or an assembly reference?)

如果我只是用项目中的代码创建一个文件,我也不会收到任何错误。据我所知,“DatabaseFacade”是 EntityFrameWorkcore.Infrastructure 的一部分,如果 Microsoft.EntityFrameworkCore.

应该是一部分

这些是我包含在 CSharpCompiler 中的参考资料:

"System"
"System.Console"
"System.Runtime"
"System.Private.CoreLib"
"System.Linq"
"System.Data.Common"
"System.Data"
"System.Data.SqlClient"
"System.ComponentModel"
"Microsoft.EntityFrameworkCore"
"Microsoft.EntityFrameworkCore.SqlServer"
"netstandard"

我正在使用 Microsoft.CodeAnalysis 创建一个 CSharpCompilation,然后发出一个我在运行时调用 main 方法的程序集。

我什至试图通过反射调用 migrate() 方法来绕过智能,但是 GetMethod("Migrate") returns 为 null,所以它显然不存在。

这是我正在尝试编译和使用运行时(简单化)的代码:

using System;
using System.Linq;
using System.Reflection;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using mothership_shared.Data;
using mothership_shared.Enums;
using mothership_shared.Interfaces;
using mothership_shared.Models;
using static mothership_shared.Attributes.PropertyCalculations;
using static mothership_shared.Attributes.PropertyTypes;
using static mothership_shared.Attributes.Settings;
using static mothership_shared.Data.SqlServiceClasses;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Infrastructure;

namespace mothership_shared.MigratorProgram
{
    public class Program
    {
        static void Main(string[] args)
        {
            SqlConnectionSettings sqlConnectionSettings = new SqlConnectionSettings()
            {
                ServerUrl = args[0],
                Catalog = args[1],
                User = args[2],
                Password = args[3],
            };
            var dbContext = new ApplicationDbContext(sqlConnectionSettings.AppConnection);
            ISql sqlService = new SqlService();
            var request = new DeleteMigrationHistory.Request()
            {
                ConnectionSettings = sqlConnectionSettings,
            };
            sqlService.DeleteMigrationHistory(request);
            dbContext.Database.Migrate();
        }
        public void Run(string[] args)
        {
            Main(args);
        }
        public class ApplicationDbContext : DbContext
        {
            private readonly string _connectionString;
            public ApplicationDbContext(string connectionString)
            {
                _connectionString = connectionString;
            }
            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                optionsBuilder.UseSqlServer(_connectionString);
            }

        public DbSet<SimpleEntity> SimpleEntity { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            var allTypes = Assembly.GetCallingAssembly().GetTypes();
            var EntityTypes = allTypes.Where(t => t.BaseType == typeof(BaseEntity));
            foreach (var t in EntityTypes)
            {
                var crud = t.GetCustomAttribute<CRUDAttribute>();
                var properties = t.GetProperties();
                foreach (var p in properties)
                {
                    IsObjectAttribute otm = p.GetCustomAttribute<IsObjectAttribute>();
                    if (otm != null)
                    {
                        if (crud.ClassType != ClassType.MTM)
                        {
                            modelBuilder.Entity(t)
                            .HasOne(p.PropertyType, p.Name)
                            .WithMany(otm.WithMany)
                            .OnDelete(otm.DeleteBehavior);
                        }
                        else
                        {
                            modelBuilder.Entity(t)
                            .HasOne(p.PropertyType, p.Name)
                            .WithMany(otm.WithMany)
                            .OnDelete(DeleteBehavior.Cascade);
                        }
                    };
                    IsTimeSpanAttribute ts = p.GetCustomAttribute<IsTimeSpanAttribute>();
                    if (ts != null)
                    {
                        modelBuilder.Entity(t)
                        .Property(p.Name)
                        .HasConversion(new TimeSpanToTicksConverter());
                    }
                }
            }
        }
    }
    [CRUDAttribute(ClassType.Referal)]
    [HeaderAttribute("Simple entity", "Simple entitys", 0)]
    [IconAttribute("ms-Icon--Document")]
    [MenuAttribute("Test menu")]
    [TooltipAttribute("This is a model that contains all simple propertytypes")]
    public class SimpleEntity : BaseEntity
    {
        [IsTextAttribute(false)]
        [HeaderAttribute("Text", "Texts", 0)]
        [PriorityAttribute(1)]
        [TooltipAttribute("This is a text property")]
        [IconAttribute("ms-Icon--Text")]
        [DefaultValueAttribute("This is the defaultvalue")]
        public string TextProperty { get; set; }
    }
}

}

您需要安装扩展程序 Microsoft.EntityFrameworkCore.Tools,因为 Migrate 是其中的一部分。如果还是不行,你可以试试dotnet restore,它通常有助于解决扩展相关的问题。