【问题标题】:Adding Migrations not working with EF Core Code first approach - C#添加迁移不适用于 EF Core 代码优先方法 - C#
【发布时间】:2023-01-30 21:55:42
【问题描述】:

我正在构建一个带有简单 CRUD 操作的 ASP.NET Web Api 和一个应该使用 EF Code First 和迁移生成的数据库。 在项目结构方面,我使用 ASP.NET Web API 作为主项目,使用类库作为数据库上下文。

这是我的类库中的内容:

数据库上下文:

using Microsoft.EntityFrameworkCore;
using MyApp.Database.Models;
namespace MyApp.Database.Context;
public class MyAppContext : DbContext
{
    public MyAppContext(DbContextOptions<MyAppContext> options) : base(options) { }
    public virtual DbSet<Booking> Bookings { get; set; }
    public virtual DbSet<Person> Persons { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Seed();
        base.OnModelCreating(modelBuilder);
    }
}

数据库模型:

namespace MyApp.Database.Models;
public class Booking
{
    public int Id { get; set; }
    public int Week { get; set; }
    public int DayOfWeek { get; set; }
    public int Hour { get; set; }
    public int PersonId { get; set; }
    public virtual Person Person { get; set; }
}
using System.Collections.Generic;
namespace MyApp.Database.Models;
public class Person
{
    public int Id { get; set; }
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    public int Age { get; set; }
    public virtual IEnumerable<Booking> Bookings { get; set; }
}

除此之外,我在库中有一个名为 SeederExtension 的类,它有一个应该为数据库播种的扩展方法

using Microsoft.EntityFrameworkCore;
using MyApp.Database.Models;
namespace MyApp.Database;
public static class SeedExtension
{
    public static void Seed(this ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Person>().HasData(new Person
        {
            Id = 1,
            Firstname = "John",
            Lastname = "Doe",
            Age = 28,

        });
    }
}

在我的 ASP.NET Web API 项目中,我使用以下代码在我的 Program.cs 中注册了数据库上下文。

builder.Services.AddDbContext<MyAppContext>(options =>
{
    options.UseSqlServer(builder.Configuration.GetConnectionString("MyAppDb"));
});

现在的问题是我想添加迁移。我转到包管理器控制台并导航到我的数据库上下文所在的类库项目。 然后我输入这个命令来添加一个迁移: dotnet ef 迁移添加“初始化” 然后会产生此错误: 无法创建“MyAppContext”类型的对象。对于设计时支持的不同模式,请参阅https://go.microsoft.com/fwlink/?linkid=851728

我看过很多教程,但不幸的是,经过数小时的研究,我还没有找到可行的解决方案。

我在我的类库中使用 .net6 和以下 NuGet 包

  • Microsoft.EntityFrameworkCore(版本 7.0.2)
  • Microsoft.EntityFrameworkCore.SqlServer(版本 7.0.2)
  • Microsoft.EntityFrameworkCor.Tools(版本 7.0.2)

如果有帮助,这是我的 connectionString:

{
  "ConnectionStrings": {
    "MyAppDb": "server=(LocalDB)\\mssqllocaldb;database=MyAppDB;integrated security=True"
  }
}

【问题讨论】:

  • 您是否注入了数据库上下文? public void ConfigureServices(IServiceCollection services) =&gt; services.AddDbContext&lt;MyAppContext&gt;(); 查看错误消息附近帖子中的链接以获取详细信息。
  • @EricJ。是的,我做到了。实际上提供代码 ;) builder.Services.AddDbContext<MyAppContext>(options => { options.UseSqlServer(builder.Configuration.GetConnectionString("MyAppDb")); });

标签: c# entity-framework asp.net-web-api ef-code-first entity-framework-migrations


【解决方案1】:

尝试添加一个无参数构造函数:public MyAppContext() {}。 选项将根据 Startup 类中的 ConfigureService 由 DI 传递给构造函数。 “添加迁移”不应该知道要传递哪些选项,因为它不使用 Startup.

【讨论】:

    【解决方案2】:

    如果你看看这里

    https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/projects?tabs=dotnet-core-cli

    请注意您的命令和选择数据库 csproj 的区别。在文档中它有:

    dotnet ef migrations add NewMigration --project WebApplication1.Migrations
    

    连接字符串来自您的主项目。同样,来自文档:

     services.AddDbContext<ApplicationDbContext>(
      options =>
        options.UseSqlServer(
            Configuration.GetConnectionString("DefaultConnection"),
            x => x.MigrationsAssembly("WebApplication1.Migrations")));
    

    显然,我刚刚从文档中粘贴了上面的内容。您将需要与自己的项目相匹配。

    如果您改为仅使用数据库项目运行迁移,我不确定它是否会从 web api 项目中提取连接字符串。

    【讨论】:

      猜你喜欢
      • 2016-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-18
      • 2012-09-15
      • 1970-01-01
      • 2019-10-23
      • 1970-01-01
      相关资源
      最近更新 更多