c# - 添加迁移时无法加载文件或程序集网络标准

标签 c# asp.net-core entity-framework-migrations .net-standard

我有一个包含 2 个项目的 ASP.Net Core 3.0 解决方案:Web(启动项目)和 Core(类库)。我尝试使用包管理器控制台创建迁移:

add-migration Initial -Project Core -StartupProject Web



并得到以下错误:

Could not load file or assembly 'netstandard, Version=2.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)



这两个项目在引用中都有 Microsoft.EntityFrameworkCore.SqlServer 和 Microsoft.EntityFrameworkCore.Tools。这就是我在 Startup.cs 中创建连接的方式:
public void ConfigureServices(IServiceCollection services)
{
    var connection = Configuration.GetConnectionString("DefaultConnection");
    services.AddDbContext<ApplicationContext>(options =>
    options.UseSqlServer(connection));
    services.AddControllersWithViews();
}

和我在 appsettings.json 中的连接字符串:
"ConnectionStrings": {
        "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=FinApp;Trusted_Connection=True;"
    }

这是我的 ApplicationContext.cs:
public class ApplicationContext : DbContext
{
    public DbSet<Account> Accounts { get; set; }
    public DbSet<Category> Categories { get; set; }
    public DbSet<Currency> Currencies { get; set; }
    public DbSet<Operation> Operations { get; set; }
    public DbSet<User> Users { get; set; }

    public ApplicationContext()
    {
    }
}

有没有人有同样的问题?

最佳答案

我认为您必须像这样定义上下文承包商:

public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}

关于c# - 添加迁移时无法加载文件或程序集网络标准,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60351698/

相关文章:

json - 在序列化时将字符串属性值转换为 base64 对于 ASP.NET Core 3.0+

asp.net-mvc - 无法使用 EF Code-First 更新数据库

c# - Postgres json_agg 包含列标题 - JSON 不正确?

c# - 将数组中的元素相乘

asp.net-core - .NET Core 6 - 如何在 Program.cs 中为依赖注入(inject)设置 ILogger(log4net)

javascript - 用于验证 ASP.NET 数据注释中格式化货币最小值的正则表达式

c# - ASP.NET:检查是否从迁移运行

entity-framework-migrations - "Cannot find the object "dbo.xxxx "because it does not exist or you do not have permissions."

java - C# 中的散列字符串不可读

c# - WithSqlFilter WITH (NOEXPAND) 提示在输出 SQL 中不正确