Nie można utworzyć obiektu typu „MyEFCoreDbContext” xaf Blazor EFCore

Nov 24 2020

Stworzyłem nowy projekt za pomocą kreatora XAF Blazor 20.2.3 i zaktualizowałem pakiety EFCore do 5.0.
Na PM pobiegłem

Install-Package Microsoft.EntityFrameworkCore.Tools

Jednak kiedy biegnę

add-migration initial

dostaję

Nie można utworzyć obiektu typu „ExamBuddyEFCoreDbContext”. Aby zapoznać się z różnymi wzorami obsługiwanymi w czasie projektowania, zobaczhttps://go.microsoft.com/fwlink/?linkid=851728

Spojrzałem na link, ale nie sądzę, aby dotyczył aplikacji xaf Blazor.

Próbowałem wykomentować inicjator bazy danych

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

[Aktualizacja]

Ustawiłem projekt modułu jako projekt początkowy, a następnie uruchomiłem

add-migration initial -verbose

wynik był

Projekt startowy „ExamBuddy.Module” jest przeznaczony dla frameworka „.NETStandard”. Nie ma środowiska uruchomieniowego skojarzonego z tą strukturą, a projekty, dla których jest przeznaczona, nie mogą być wykonywane bezpośrednio. Aby użyć narzędzi konsoli Entity Framework Core Package Manager w tym projekcie, dodaj plik wykonywalny przeznaczony dla platformy .NET Framework lub .NET Core, który odwołuje się do tego projektu, i ustaw go jako projekt startowy; lub zaktualizuj ten projekt, aby obejmował wiele docelowych programów .NET Framework lub .NET Core. Aby uzyskać więcej informacji na temat używania narzędzi EF Core z projektami .NET Standard, zobaczhttps://go.microsoft.com/fwlink/?linkid=2034705

[Aktualizacja]

Łączenie z biletem Dev Express

Dodałem następujące elementy do 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);
        }
    }

Eksperymentowanie w konsoli menedżera pakietów i dodanie ConsoleApp1 mam

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

Dodałem więc ConsoleApp1 jako projekt startowy 3.1 .net core i dodałem odniesienia do projektów XAF

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

Aby zapoznać się z różnymi wzorami obsługiwanymi w czasie projektowania, zobacz https://go.microsoft.com/fwlink/?linkid=851728

Źródło znajduje się na GitHub

Odpowiedzi

1 KirstenGreed Nov 30 2020 at 15:37

Utworzyłem klasę ExamBuddyContextFactory zgodnie z zaleceniami zawartymi w zgłoszeniu DevExpress w projekcie Module, a następnie mogłem utworzyć początkową migrację.

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