简体   繁体   中英

.NET 6 Single-File ASP.NET template with Entity Framework Core

I am using the Visual Studio 2022/.NET 6 Razor pages template. It uses a top-level C# 10 program file. Program.cs looks something like this:

using Microsoft.EntityFrameworkCore;
using Data;
using Services;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddMemoryCache();
builder.Services.AddRazorPages();

var settings = builder.Configuration.Get<AppSettings>();

builder.Services
    .AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));

var app = builder.Build();
app.UseHttpsRedirection();
app.UseRouting();
app.MapRazorPages();
app.Run();

My DBContext looks like this

public class ApplicationDbContext: DbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
    internal DbSet<MyModel> MyModels { get; set; }
    protected override void OnModelCreating(ModelBuilder builder){ /* ... */ }
}

When I run do.net ef migrations add InitialMigration -v , I get the following error:

Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider in assembly 'Data'...
Finding Microsoft.Extensions.Hosting service provider...
No static method 'CreateHostBuilder(string[])' was found on class 'Program'.
No application service provider was found.
Finding DbContext classes in the project...
Found DbContext 'ApplicationDbContext'.
Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type 'ApplicationDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
 ---> System.InvalidOperationException: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[Data.DatabaseContext.ApplicationDbContext]' while attempting to activate 'Data.DatabaseContext.ApplicationDbContext'.

Visual Studio Add-Migration also fails to build, but provides no stack trace.

The provided link indicated that I might be able to add an implementation of IDesignTimeDbContextFactory to contain some startup code that might work.

I created this class, but it also seems to do nothing.

public class ApplicationContext: IDesignTimeDbContextFactory<ApplicationDbContext>
{
    ApplicationDbContext IDesignTimeDbContextFactory<SanDiegoContext>.CreateDbContext(string[] args)
    {
        IConfigurationRoot configuration = MyStaticConfigurationBuilder.GetConfig();

        var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
        optionsBuilder.UseSqlServer(connectionString: configuration.GetConnectionString("DefaultConnection"));

        return new ApplicationDbContext(optionsBuilder.Options);
    }
}

Is it possible to use EF Core in .NET 6 without building a GetHostBuilder(string[]) method and a Startup class? If so, what am I doing wrong?

I made it work with the following steps:

  1. Rebuild the project.
  2. Make sure I was using Visual Studio Command Line Tools with the right project
  3. Install Microsoft.EntityFrameworkCore.Design to my Web project
  4. Make sure my project builds on its own with a standard CTRL-B or do.net build

Then my startup looks like this:

using Microsoft.EntityFrameworkCore;
using Data;
using Services;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddMemoryCache();
builder.Services.AddRazorPages();

var settings = builder.Configuration.Get<AppSettings>();

builder.Services
    .AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));

var app = builder.Build();
app.UseHttpsRedirection();
app.UseRouting();
app.MapRazorPages();
app.Run();

and the migration attempts to scaffold as usual

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM