【问题标题】:Unable to create an object of type 'MyEFCoreDbContext' xaf Blazor EFCore无法创建“MyEFCoreDbContext”xaf Blazor EFCore 类型的对象
【发布时间】:2021-03-07 15:31:45
【问题描述】:

我使用 XAF Blazor 20.2.3 向导创建了一个新项目 并将 EFCore 包升级到 5.0。
在下午我跑了

Install-Package Microsoft.EntityFrameworkCore.Tools

但是当我跑步时

add-migration initial

我明白了

无法创建“ExamBuddyEFCoreDbContext”类型的对象。有关设计时支持的不同模式,请参阅https://go.microsoft.com/fwlink/?linkid=851728

我查看了该链接,但我认为它不适用于 xaf Blazor 应用程序。

我尝试注释掉数据库初始化器

using System;
using DevExpress.ExpressApp.EFCore.Updating;
using Microsoft.EntityFrameworkCore;
using DevExpress.Persistent.BaseImpl.EF.PermissionPolicy;
using DevExpress.Persistent.BaseImpl.EF;
using DevExpress.ExpressApp.Design;
using DevExpress.ExpressApp.EFCore.DesignTime;
using DevExpress.Persistent.Base;
namespace ExamBuddy.Module.BusinessObjects {
    public class ExamBuddyContextInitializer : DbContextTypesInfoInitializerBase {
        protected override DbContext CreateDbContext() {
            var builder = new DbContextOptionsBuilder<ExamBuddyEFCoreDbContext>()
                .UseSqlServer(@";");
            return new ExamBuddyEFCoreDbContext(builder.Options);
        }
    }
    //[TypesInfoInitializer(typeof(ExamBuddyContextInitializer))]
    public class ExamBuddyEFCoreDbContext : DbContext {
        public ExamBuddyEFCoreDbContext(DbContextOptions<ExamBuddyEFCoreDbContext> options) : base(options) {
        }
        public DbSet<ModuleInfo> ModulesInfo { get; set; }
        public DbSet<ModelDifference> ModelDifferences { get; set; }
        public DbSet<ModelDifferenceAspect> ModelDifferenceAspects { get; set; }
        public DbSet<PermissionPolicyRole> Roles { get; set; }
        public DbSet<PermissionPolicyRoleBase> RolesBase { get; set; }
        public DbSet<PermissionPolicyUser> Users { get; set; }
        public DbSet<CourseUnit> CourseUnits { get; set; }
         
    }

[更新]

我将模块项目设置为启动项目然后我运行

add-migration initial -verbose

结果是

启动项目“ExamBuddy.Module”以框架“.NETStandard”为目标。该框架没有关联的运行时,不能直接执行针对它的项目。要在此项目中使用 Entity Framework Core 包管理器控制台工具,请添加一个针对 .NET Framework 或引用此项目的 .NET Core 的可执行项目,并将其设置为启动项目;或者,将此项目更新为跨目标 .NET Framework 或 .NET Core。有关在 .NET Standard 项目中使用 EF Core 工具的更多信息,请参阅https://go.microsoft.com/fwlink/?linkid=2034705

[更新]

交叉链接到the Dev Express ticket

我在 ExamBuddy.Blazor.Server 中添加了以下内容

    public class ExamBuddyContextFactory : IDesignTimeDbContextFactory<ExamBuddyEFCoreDbContext>
    {
        public ExamBuddyEFCoreDbContext CreateDbContext(string[] args)
        {
            var optionsBuilder = new DbContextOptionsBuilder<ExamBuddyEFCoreDbContext>();
            optionsBuilder.UseSqlServer(@"Integrated Security=SSPI;MultipleActiveResultSets=true;Pooling=false;Data Source=(localdb)\\mssqllocaldb;Initial Catalog=ExamBuddy;ConnectRetryCount=0;");
            return new ExamBuddyEFCoreDbContext(optionsBuilder.Options);
        }
    }

在包管理器控制台中进行实验并添加我拥有的 ConsoleApp1

PM> add-migration init
Build started...
Build succeeded.
Startup project 'ExamBuddy.Module' targets framework '.NETStandard'. There is no runtime associated with this framework, and projects targeting it cannot be executed directly. To use the Entity Framework Core Package Manager Console Tools with this project, add an executable project targeting .NET Framework or .NET Core that references this project, and set it as the startup project; or, update this project to cross-target .NET Framework or .NET Core. For more information on using the EF Core Tools with .NET Standard projects, see https://go.microsoft.com/fwlink/?linkid=2034705

所以我将 ConsoleApp1 添加为 3.1 .net 核心启动项目并添加了对 XAF 项目的引用

PM> add-migration init
Build started...
Build succeeded.
Unable to create an object of type 'ExamBuddyEFCoreDbContext'. 

有关设计时支持的不同模式,请参阅https://go.microsoft.com/fwlink/?linkid=851728

来源是on GitHub

【问题讨论】:

    标签: entity-framework-core entity-framework-migrations blazor-server-side xaf


    【解决方案1】:

    我按照 DevExpress 票证中的建议创建了 ExamBuddyContextFactory 类 在 Module 项目中,然后能够创建初始迁移。

    public class ExamBuddyContextFactory : IDesignTimeDbContextFactory<ExamBuddyEFCoreDbContext>
    {
        public ExamBuddyEFCoreDbContext CreateDbContext(string[] args)
        {
            var optionsBuilder = new DbContextOptionsBuilder<ExamBuddyEFCoreDbContext>();
            optionsBuilder.UseSqlServer(@"Integrated Security=SSPI;MultipleActiveResultSets=true;Pooling=false;Data Source=(localdb)\\mssqllocaldb;Initial Catalog=ExamBuddy;ConnectRetryCount=0;");
            return new ExamBuddyEFCoreDbContext(optionsBuilder.Options);
        }
    }
    

    【讨论】:

    • 但是我在尝试运行更新数据库时遇到错误。指定的 LocalDB 实例名称无效
    • 这来自 DevEx 支持,真的吗?使用逐字字符串时,应使用单反斜杠,即(localdb)\mssqllocaldb,否则指定的LocalDB实例名称无效
    • appsettings.json 有 "ConnectionString": "Integrated Security=SSPI;Pooling=false;Data Source=(localdb)\\mssqllocaldb;Initial Catalog=ExamBuddy" 并且工作正常。
    猜你喜欢
    • 2021-02-06
    • 2021-11-10
    • 2020-11-09
    • 2021-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多