Objekt vom Typ 'MyEFCoreDbContext' xaf Blazor EFCore kann nicht erstellt werden

Nov 24 2020

Ich habe mit dem XAF Blazor 20.2.3-Assistenten ein neues Projekt erstellt und die EFCore-Pakete auf 5.0 aktualisiert.
In PM rannte ich

Install-Package Microsoft.EntityFrameworkCore.Tools

Jedoch wenn ich renne

add-migration initial

Ich bekomme

Ein Objekt vom Typ 'ExamBuddyEFCoreDbContext' kann nicht erstellt werden. Informationen zu den verschiedenen Mustern, die zur Entwurfszeit unterstützt werden, finden Sie unterhttps://go.microsoft.com/fwlink/?linkid=851728

Ich habe mir den Link angesehen, aber ich glaube nicht, dass er für die xaf Blazor-App gilt.

Ich habe versucht, den Database Initializer auskommentieren

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

[Aktualisieren]

Ich habe das Modulprojekt als Startprojekt festgelegt und dann ausgeführt

add-migration initial -verbose

Das Ergebnis war

Das Startprojekt 'ExamBuddy.Module' zielt auf das Framework '.NETStandard' ab. Diesem Framework ist keine Laufzeit zugeordnet, und Projekte, die darauf abzielen, können nicht direkt ausgeführt werden. Fügen Sie ein ausführbares Projekt für .NET Framework oder .NET Core hinzu, das auf dieses Projekt verweist, und legen Sie es als Startprojekt fest, um die Konsolentools von Entity Framework Core Package Manager für dieses Projekt zu verwenden. oder aktualisieren Sie dieses Projekt auf .NET Framework oder .NET Core. Weitere Informationen zur Verwendung der EF Core Tools mit .NET Standard-Projekten finden Sie unterhttps://go.microsoft.com/fwlink/?linkid=2034705

[Aktualisieren]

Querverbindung zum Dev Express Ticket

Ich habe dem ExamBuddy.Blazor.Server Folgendes hinzugefügt

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

Experimentieren in Package Manager Console und Hinzufügen von ConsoleApp1 habe ich

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

Daher habe ich ConsoleApp1 als 3.1 .net-Kernstartprojekt hinzugefügt und Verweise auf die XAF-Projekte hinzugefügt

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

Informationen zu den verschiedenen Mustern, die zur Entwurfszeit unterstützt werden, finden Sie unter https://go.microsoft.com/fwlink/?linkid=851728

Die Quelle ist auf GitHub

Antworten

1 KirstenGreed Nov 30 2020 at 15:37

Ich habe die ExamBuddyContextFactory-Klasse wie im DevExpress-Ticket im Modulprojekt angegeben erstellt und konnte dann die erste Migration erstellen.

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