标签: entity-framework-migrations

无法创建类型为“[DBContext 的名称]”的对象。对于设计时支持的不同模式

我正在 Udemy 中学习有关 ASP.NET MVC 的 Mosh Hamedani 课程之一。

我在使用代码优先(实体框架)设计数据库时遇到了一个错误。

起初,我收到了“在程序集中找不到 DbContext”的错误消息。解决了这个问题后,又一个突然冒出来了。

下图将显示添加迁移时发现的错误。我已经搜索了相同的错误,但没有找到。我在过去的两个小时里一直在挣扎,但到目前为止还没有解决任何问题。

请有人帮助我。谢谢

无法创建“Vidly_Context”类型的对象。有关设计时支持的不同模式,请参阅https://go.microsoft.com/fwlink/?linkid=851728


使用 (2) 参数添加自己的 DbContext 构造函数后出现类似问题。应用程序正常,但迁移停止工作。通过使用来自Dotnet 工具 @xspdf 的信息并通过方法 + 硬编码默认连接字符串(如果未设置)替换提到的构造函数,通过第一次更新 EF(使用 5 时出于奇怪原因使用 3.1.5)来修复

dotnet tool update --global dotnet-ef

// following command show the most during migration build/run in cmd
// mind current dir is Migrations folder of (VS) startup project here
dotnet ef --startup-project ../ --verbose migrations add test
Run Code Online (Sandbox Code Playgroud)

3.1.5 & 上下文激活错误

The Entity Framework …
Run Code Online (Sandbox Code Playgroud)

asp.net asp.net-mvc entity-framework entity-framework-core entity-framework-migrations

56
推荐指数
9
解决办法
6万
查看次数

在 asp.net core 6 应用程序的启动时运行 EF 迁移

如何在 ASP.NET 6 应用程序启动时运行 ef 迁移。

这是我的Program.cs

var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
var serverVersion = new MySqlServerVersion(new Version(8, 0, 23));
builder.Services.AddDbContext<MyContext>(x => x.UseMySql(connectionString, serverVersion)
            .LogTo(Console.WriteLine, LogLevel.Information)
            .EnableSensitiveDataLogging()
            .EnableDetailedErrors());
Run Code Online (Sandbox Code Playgroud)

如何在这里执行 MyContext.Database.Migrate() ?

c# entity-framework asp.net-core entity-framework-migrations

18
推荐指数
2
解决办法
1万
查看次数

为什么“dotnet ef migrations add”会启动我的应用程序?

我已经使用 .NET 6 和 EF Core 6 创建了一个新项目,但是当我尝试使用 创建迁移时dotnet ef migrations add,CLI 在“构建成功”后开始运行应用程序。

这不会阻止创建迁移,但会增加该过程的大量时间,因为我需要等待应用程序退出才能发出任何其他命令。

dotnet ef 迁移的片段添加输出:

dotnet ef 迁移的片段添加输出

在同一解决方案中,我还有许多其他使用 .NET 5 和 EF Core 5 的项目,它们都按预期创建迁移(仅构建项目,不运行它)。

这是 EF Core 6 的新功能吗?或者是否有某个配置会导致此行为?

编辑

问题似乎是 EF Core 在迁移创建期间忽略了 IDesignTimeDbContext 工厂的实现,而是默认从主机构建器获取 DbContext 的实例。显然 EF Core 5 也可能发生这种情况。

dotnet/efcore github 上有一个未解决的问题:Migrations script not using IDesignTimeDbContextFactory

c# entity-framework-migrations asp.net-core-6.0 .net-6.0 ef-core-6.0

13
推荐指数
2
解决办法
2711
查看次数

如何使用 EFCore Code First Migrations 指定复合主键

我正在使用 Asp.Net Core 2.1、Mvc、c#、EF Core with Code First 和 Migrations。

我正在尝试构建一个在Migration.Up()方法中具有复合主键的表:

migrationBuilder.CreateTable(
    name: "TagValueAttributes",
    columns: table => new {
        TagValueID = table.Column<Int64>(nullable: false),
        Identifier = table.Column<string>(nullable: false, unicode: true, maxLength: 256),
        Value = table.Column<string>(nullable: true, unicode: true, maxLength: 2048)
    },
    constraints: table => {
        table.PrimaryKey(
            name: "PK_TagValueAttributes",
            columns: // what goes here???
        )
    }
);
Run Code Online (Sandbox Code Playgroud)

我不知道为调用的columns参数指定什么constraints table.PrimaryKey()。我想要 columns TagValueID,并Identifier形成复合键。

我需要为columns参数指定什么?

composite-primary-key asp.net-core asp.net-core-2.1 ef-core-2.1 entity-framework-migrations

11
推荐指数
2
解决办法
1万
查看次数

实体框架迁移 Azure DevOps 发布管道

我正在尝试在 Azure DevOps Release Pipeline 上运行迁移。因为我想在每个版本上自动运行我的数据库脚本。我的发布管道没有源代码,我只是编译了 DLL。

当我在本地机器上执行此命令时,它运行成功。如何转换此命令以便我可以将它与 DLL 一起使用。

dotnet ef database update --project MyEntityFrameworkProject --context MyDbContext --startup-project MyStartupProject

entity-framework azure-devops azure-pipelines azure-pipelines-release-pipeline entity-framework-migrations

10
推荐指数
2
解决办法
9307
查看次数

Entity Framework creates migration without any updates

At the moment, I'm experiencing an issue where I can create a migration, which has data updates, but the DbContextModelSnapshot has this data. When you look at the updates in the migration, they've already been applied to the DbContextModelSnapshot and the database.

A coworker created a migration about 3 months ago, and everything has been fine. We've created several more migrations since then, and haven't experienced this issue. However, the last couple days, we have been experiencing this.

We've tried …

c# entity-framework-core entity-framework-migrations ef-core-3.1

10
推荐指数
1
解决办法
231
查看次数

如何修复 EF Core 迁移“证书链由不受信任的机构颁发”

连接字符串:

Data Source=<server>,<port>;Initial Catalog=<database>;User Id=<username>;
Password=<password>?;MultipleActiveResultSets=true;Encrypt=True;
TrustServerCertificate=false;Integrated Security=false;Trusted_Connection=true;
Connection Timeout=30;
Run Code Online (Sandbox Code Playgroud)

我的连接字符串包含:

TrustServerCertificate=false
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

已成功与服务器建立连接,但登录过程中出现错误。(提供程序:SSL 提供程序,错误:0 - 证书链是由不受信任的机构颁发的。)

当我有一个连接字符串时:

TrustServerCertificate=true
Run Code Online (Sandbox Code Playgroud)

我得到:

用户“<用户名>”登录失败。// <-- 删除真实用户名

entity-framework entity-framework-core entity-framework-migrations

10
推荐指数
2
解决办法
9037
查看次数

创建编辑器失败。HRESULT 异常:0x80070057 (E_INVALIDARG)

创建编辑器失败。参数不正确。(HRESULT 异常:0x80070057 (E_INVALIDARG))

\n

错误

\n

脚本迁移 - 来自“PreviousMigration”

\n

将 Visual Studio Community 2022 从版本 17.8.6 更新到 17.9.0 导致错误。

\n

脚本迁移:调用 COM 组件返回了错误 HRESULT E_FAIL。

\n

行:1 字符:1

\n

**解决方案:

\n

我尝试从系统文件夹中删除用户和框架的所有临时文件,但它对我不起作用,然后我将xc2xa0降级到以前的xc2xa0Visual Studio Community 2022 版本 17.8.6,它可以工作。

\n

我尝试从系统文件夹中删除用户和框架的所有临时文件,但它对我不起作用,然后我将xc2xa0降级到以前的xc2xa0Visual Studio Community 2022 版本 17.8.6,它可以工作。

\n

visual-studio entity-framework-core entity-framework-migrations ef-core-6.0

10
推荐指数
1
解决办法
468
查看次数

在 Xamarin Android 项目中使用 EF Core 创建迁移

我尝试在 Xamarin.Android 项目中使用 EFCore 和 SQLite。我还添加了 Microsoft.EntityFrameworkCore.Tools 包。当我尝试创建数据库迁移时,出现各种错误。

dotnet ef migrations add migrationname命令行上运行时出现错误

找不到与命令“dotnet-ef”匹配的可执行文件

Add-Migration migrationname在包管理器控制台中运行时出现错误

启动项目“MyProject”针对框架“MonoAndroid”。实体框架核心包管理器控制台工具不支持此框架。

有没有办法解决这个问题?我是否必须使用 EF6 而不是

xamarin.android entity-framework-core entity-framework-migrations

9
推荐指数
1
解决办法
3342
查看次数

如何在 ASP.NET Core 2.1 中使用 IDesignTimeDbContextFactory 实现?

我有 ASP.NET Core 1.1 项目,我正在尝试将其转换为 v2.1。我找到了IDesignTimeDbContextFactory实现,但我不明白我应该在哪里创建这个工厂的实例以及如何将它用于迁移正常工作。现在在创建/删除迁移之前,我必须清理并重建解决方案,因为如果没有,我会收到以下消息:Unable to create an object of type 'MyDbContext'. Add an implementation of 'IDesignTimeDbContextFactory<MyDbContext>' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time。所有上下文数据都在单独的项目中。如何使用IDesignTimeDbContextFactory实现或我做错了什么?

asp.net-core-2.1 entity-framework-migrations

9
推荐指数
1
解决办法
9656
查看次数