按照我的问题实体框架迁移:为什么它会忽略快照并考虑 __MigrationHistory?,我很困惑为什么 EF 迁移将以前的模型同时存储在 __MigrationHistory 表中的 DB 和资源文件中?而更疑惑的是为什么完全忽略资源,考虑表数据来计算模型变化?
最近从 EF Core 1.0 迁移到 EF Core 2.0,并且运行良好。今天我添加了一个新表(代码优先)并将其添加到我的 DbContext 中:
public virtual DbSet<Foo> Foos { get; set; }
Run Code Online (Sandbox Code Playgroud)
当我运行迁移时,我的 DbContext 在一个单独的项目中(请记住,这之前都是有效的),迁移结束,但没有创建迁移文件。这一切都在 Core 2.0 之前使用:
http://paultechguy.blogspot.com/2017/04/entity-framework-core-migrating-and.html
PM> Add-Migration GradePremade -project Mfc.MfcRepositoryModel -verbose -context MfcDbContext -StartupProject web.xyz
Using project 'class.xyz\Mfc.MfcRepositoryModel'.
Using startup project 'web.xyz'.
Build started...
Build succeeded.
C:\Program Files\dotnet\dotnet.exe exec --depsfile C:\development\xyz\web.xyz\bin\Debug\netcoreapp2.0\web.xyz.deps.json --additionalprobingpath C:\Users\pcarver\.nuget\packages --additionalprobingpath "C:\Program Files\dotnet\sdk\NuGetFallbackFolder" --runtimeconfig C:\development\xyz\web.xyz\bin\Debug\netcoreapp2.0\web.xyz.runtimeconfig.json "C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.entityframeworkcore.tools\2.0.0\tools\netcoreapp2.0\ef.dll" migrations add GradePremade --json --context MfcDbContext --verbose --no-color --prefix-output --assembly C:\development\xyz\web.xyz\bin\Debug\netcoreapp2.0\Mfc.MfcRepositoryModel.dll --startup-assembly C:\development\xyz\web.xyz\bin\Debug\netcoreapp2.0\web.xyz.dll --project-dir C:\development\xyz\class.xyz\Mfc.MfcRepositoryModel --root-namespace Mfc.MfcRepositoryModel
Using assembly 'Mfc.MfcRepositoryModel'. …Run Code Online (Sandbox Code Playgroud) c# entity-framework database-migration entity-framework-core entity-framework-migrations
我如何使用DbContext它适用于当前数据库(现在在迁移中使用)。
例子:
namespace Data.SqlServer.Migrations
{
[DbContext(typeof(MyDbContext))] // I want use this context
[Migration("CustomMigration_DataSeed")]
public partial class DataSeedMigration : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
// add some entities
_context.User.Add(new User());
}
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
感谢帮助!
.net c# migration entity-framework entity-framework-migrations
我正在尝试在已创建数据库的现有系统上构建一个 aspnetcore 应用程序,我将在其上添加一些表。
我对数据库进行了逆向工程,将现有表作为实体添加到我的应用程序中,并且我编写了自己的实体,稍后将添加这些实体。最后,所有实体都添加到单个 DbContext 中。
我的要求如下:
注意:我不想接触生成的迁移代码。
有没有合适的方法来处理这种情况?
entity-framework ef-database-first entity-framework-migrations ef-core-2.2
我正在使用 .NET Core 3.1 和 EntityFramework Core 3.1.3。我正在尝试使用 DB 模式实现租户数据分离。我读了这个。我知道它有点过时了,所以我已经调整了。
我已经创建了一个 DbContext 的实现:
public class AppDataContext : DbContext
{
private readonly ITenantProvider _tenantProvider;
public AppDataContext(DbContextOptions<AppDataContext> options, ITenantProvider tenantProvider) : base(options)
{
_tenantProvider = tenantProvider;
}
public DbSet<Book> Books { get; set; }
public DbSet<Comics> Comics { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Tenant schema mapping
var tenant = _tenantProvider.GetTenantString();
modelBuilder.HasDefaultSchema(tenant);
}
public static void ApplyMigrations(string connectionString, string tenant)
{
var optionsBuilder = new DbContextOptionsBuilder<AppDataContext>(); …Run Code Online (Sandbox Code Playgroud) c# sql-server entity-framework-core .net-core entity-framework-migrations
我在 VS2019 的开发人员 PowerShell 中使用 EF Core 工具时出现奇怪的行为。
使用此命令创建迁移后:
dotnet ef migrations add VisibleLink -p .\src\Only.Portal.Data\ -s .\src\Only.Portal.Web
Run Code Online (Sandbox Code Playgroud)
它导致启动我的应用程序,但以前没有。应用最后迁移的方法,这会导致dotnet ef migrations remove完全损坏,因为使用它时,它首先启动一个应用程序,然后调用`Migrate()。
然后我收到一条消息:
迁移“20220128090939_VisibleLink”已应用于数据库。恢复并重试。如果迁移已应用于其他数据库,请考虑使用新的迁移来恢复其更改
看起来像是死循环。
c# entity-framework-core entity-framework-migrations ef-core-6.0
我在每次迁移时都会遇到种子角色的奇怪行为。无论您做了什么更改,迁移都会删除种子角色并再次插入它们。当项目中没有进行任何修改时,将创建下面给出的迁移。
所有其他模型均已正确播种,并且仅在修改它们时才考虑迁移。
我将 ASP .NET Core 2.1 与个人身份验证一起使用
用于播种的 DbContext 类
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
#region Seed-Roles
modelBuilder.Entity<IdentityRole>().HasData(new IdentityRole { Name = "SuperAdmin", NormalizedName = "SuperAdmin".ToUpper() });
modelBuilder.Entity<IdentityRole>().HasData(new IdentityRole { Name = "Owner", NormalizedName = "Owner".ToUpper() });
modelBuilder.Entity<IdentityRole>().HasData(new IdentityRole { Name = "Admin", NormalizedName = "Admin".ToUpper() });
modelBuilder.Entity<IdentityRole>().HasData(new IdentityRole { Name = "Tester", NormalizedName = "Tester".ToUpper() });
modelBuilder.Entity<IdentityRole>().HasData(new IdentityRole { Name = "User", NormalizedName …Run Code Online (Sandbox Code Playgroud) c# asp.net-core-identity asp.net-core-2.1 ef-core-2.1 entity-framework-migrations
所以我想以编程方式使用迁移,为此我做了这样的事情。
Seed 是我通过 StoreContext 创建的扩展方法
StoreContext 是我的 DbContext
internal StoreContext(DbContextOptions options)
: base(options)
{
RelationalDatabaseCreator creator = this.GetService<IDatabaseCreator>() as RelationalDatabaseCreator;
if (!creator.Exists())
{
creator.Create(); ///=> create database
creator.CreateTables(); ///=> create database tables
}
Database.Migrate(); ///=> apply migrations
if (creator.Exists())
{
this.Seed();
}
}
Run Code Online (Sandbox Code Playgroud)
如果我运行时数据库不存在
添加迁移初始
一切都很好(这仅适用于我的第一次迁移)
如果我决定添加新的迁移
Add-Migration Add_Student_FirstName
学生中的一个新字段然后我得到这个 PMC 异常
数据库中已经有一个名为“SomeTableName”的对象。
控制台抱怨的 SomeTableName 不一定是我添加更改的表。
但是,如果我进入我的代码并注释该行
Database.Migrate(); ///=> apply migrations
Run Code Online (Sandbox Code Playgroud)
并再次运行
Add-Migration Add_Student_FirstName
一切都很好(我的迁移已添加到 Migrations 文件夹,但我仍然必须使用
更新数据库
命令)
那么我正在做的事情有什么问题,我应该如何解决这个问题,以便在我创建迁移时自动应用它?
谢谢
entity-framework entity-framework-core .net-core asp.net-core entity-framework-migrations
我想在 Azure Devops 中为我的 .Net Core 应用程序添加构建和发布管道。应用程序使用 EF Core 迁移。
我使用了这里提供的解决方案:https : //blog.clear-measure.com/2019/01/07/run-ef-core-migrations-in-azure-devops/
它工作正常。我在构建期间生成脚本,将其作为另一个工件发布,然后在发布步骤中使用它来针对数据库运行。
但是如何处理我想恢复更改的情况。在这里我看到两个场景:
如果我能以某种方式在以前的版本上单击 Deploy 并以这种方式返回到应用程序的以前的工作版本(并恢复数据库),那就太好了。
恢复 repo 中的更改,删除可能已在发布之间创建的迁移并再次启动构建和部署过程。如何将已添加的迁移恢复到数据库?
甚至第一种方法可能吗?
谢谢你的帮助!
continuous-integration entity-framework-core azure-devops entity-framework-migrations
我的 SQL 表有两个字段:Id 和 Status。它看起来像这样
身份证 | 地位
1 | “状态 1”
2 | “状态2”
我应该进行迁移,将这些状态值更改为我想要的状态值吗?我怎样才能做到这一点?
database-migration entity-framework-core asp.net-core entity-framework-migrations
entity-framework-migrations ×10
c# ×5
.net-core ×2
asp.net-core ×2
.net ×1
azure-devops ×1
ef-core-2.1 ×1
ef-core-2.2 ×1
ef-core-6.0 ×1
migration ×1
sql-server ×1