繁体   English   中英

添加迁移不适用于 EF Core 代码优先方法 - C#

[英]Adding Migrations not working with EF Core Code first approach - C#

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

这是我的 Class 库中的内容:

数据库上下文:

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 的库中有一个 class,它有一个应该为数据库播种的扩展方法

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

现在的问题是我想添加迁移。 我 go 到 Package 管理器控制台并导航到我的数据库上下文所在的 Class 库项目。 然后我输入此命令以添加迁移:do.net ef migrations add "Init" 会产生此错误:无法创建类型为“MyAppContext”的 object。 有关设计时支持的不同模式,请参阅https://go.microsoft.com/fwlink/?linkid=851728

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

我在我的 class 库中使用 .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 MyAppContext() {} 选项将由 DI 根据 Startup class 中的 ConfigureService 传递给构造函数。“添加迁移”不应该知道要传递哪些选项,因为它不使用 Startup。

如果你看看这里

https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/projects?tabs=do.net-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 项目中提取连接字符串。

问题未解决?试试以下方法:

添加迁移不适用于 EF Core 代码优先方法 - C#

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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