当我想要创建或更改存储过程和视图时,我通过将 sql 放入如下语句中来避免“应该是批处理文件中的第一个语句”错误:EXEC
Sql(EXEC('Alter View dbo.Foos As etc'))
Run Code Online (Sandbox Code Playgroud)
这一切都很好,但这确实意味着我必须一直逃避撇号。
我最近意识到DbMigration.Sql方法采用一个名为 的布尔参数suppressTransaction。文档中是这样描述的:
指示 SQL 是否应在用于迁移过程的事务之外执行的值。
所以我在没有使用的情况下对其进行了测试EXEC:
例如Sql('Create View dbo.Foos As etc', true);
它有效,但我担心的是这个。如果我在迁移过程中的其他地方犯了错误怎么办?我假设其他所有内容都会回滚,但这个 Sql 不会。我最终会导致数据库处于不确定状态吗?如果这是真的,这个参数有什么用呢?
我在我的解决方案中使用有界上下文 (BC) 和 EF Code-first 来生成数据库。
有些表被部分地(不是所有字段)定义为不同 BC 中的类,以便为相关表添加外键,但总是有一个 BC 具有一个类,该类通过单个类定义具有所有字段的表。
我没有找到基于多个上下文生成单个迁移的方法,因此我不得不添加多个迁移。但是在每次迁移中,EF 都会添加 BC 中所有类的所有更改,包括部分定义的表。最大的问题是:
我正在工作的项目有十几个奇怪的开发人员、350 多个表和 20 多个 BC,因此手动更改 EF Migration 生成的脚本听起来很疯狂。
似乎解决它的唯一方法是更改迁移管道以排除由标记有特定属性的类定义的表的创建或更改。
那么,有没有其他方法可以解决我的问题,或者我应该破解 EF 源代码并在那里注入旁路例程?
我正在尝试通过实体框架核心迁移更改表的主键:
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropPrimaryKey(
name: "PK_Permissions",
table: "Permissions");
}
Run Code Online (Sandbox Code Playgroud)
当我尝试更新数据库时,我收到以下错误消息:
To change the IDENTITY property of a column, the column needs to be dropped and recreated.
Run Code Online (Sandbox Code Playgroud)
如何更新数据库?
primary-key entity-framework-core entity-framework-migrations
我有一个多项目解决方案,我无法从主项目成功添加数据库迁移University.Application。数据库上下文在程序集中定义University.Infrastructure。
当尝试dotnet ef migrations add Initial在 University.Application 项目目录中时,我收到以下错误:
Your target project 'University.Application' doesn't match your migrations assembly 'University.Infrastructure'. Either change your target project or change your migrations assembly.
Change your migrations assembly by using DbContextOptionsBuilder. E.g. options.UseSqlServer(connection, b => b.MigrationsAssembly("University.Application")). By default, the migrations assembly is the assembly containing the DbContext.
Change your target project to the migrations project by using the Package Manager Console's Default project drop-down list, or by executing "dotnet ef" …
entity-framework-core asp.net-core entity-framework-migrations
我正在使用 .Net Core 2.1 和 EF Core 2.1。我使用 EF Code First Migrations 成功创建了 2 个表,dotnet CLI 也将数据植入其中。但是,当我尝试添加新模型功能并运行迁移时,生成的文件具有空的 Up() 和 Down() 方法,并且数据库的 EF_MigrationsHistory 表中没有条目,并且 modelSnapshot.cs 文件也没有包含对特征模型的任何引用。我交叉检查了 ApplicationDbContext 以查看我是否不小心错过了类中模型的声明,但我没有。我不确定这个问题。谁能帮我解决这个问题?发布我的项目中的代码:
功能.cs
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace ProjectName.Models
{
public class Feature
{
public int Id { get; set; }
[Required]
[StringLength(255)]
public string Name { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
应用程序数据库上下文.cs
using Microsoft.EntityFrameworkCore;
using ProjectName.Models;
namespace ProjectName.Persitance{
public class ApplicationDbContext: DbContext{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> context)
: base(context){ }
public DbSet<Make> Makes { get; set; …Run Code Online (Sandbox Code Playgroud) 我在每次迁移时都会遇到种子角色的奇怪行为。无论您做了什么更改,迁移都会删除种子角色并再次插入它们。当项目中没有进行任何修改时,将创建下面给出的迁移。
所有其他模型均已正确播种,并且仅在修改它们时才考虑迁移。
我将 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
我正在尝试运行update-database以迁移我对数据库所做的一些更改。
一切顺利,直到出现以下错误:
找不到适合实体类型“ReportType”的构造函数。以下构造函数的参数无法绑定到实体类型的属性:无法绑定 'id', 'name' in 'ReportType(string id, string name)'。
这是 ReportType.cs 的代码:
public class ReportType : SmartEnum<ReportType, string>
{
public static readonly ReportType ReportType1 = new ReportType("Blah", "Blah");
public static readonly ReportType ReportType2 = new ReportType("Blah", "Blah");
public static readonly ReportType ReportType3 = new ReportType("Blah", "Blah");
// required for EF, but breaking for SmartEnum
// private ReportType() {}
private ReportType(string id, string name) : base(name, id)
{
}
}
Run Code Online (Sandbox Code Playgroud)
正如您在该代码的注释部分所看到的,拥有无参数构造函数通常可以解决 EF Core 的这个问题,但 SmartEnum 没有无参数构造函数基础。
2018 年 …
c# entity-framework entity-framework-core asp.net-core entity-framework-migrations
假设我总共有 3 个迁移:A、B 和 C。所有迁移都内置于代码中并从代码中执行(我的意思是 C#)。
我想将所有迁移应用到给定级别(例如 B),让我重新控制,这样我就可以在 EF/migrations/etc 范围之外做任何我喜欢的事情。当我完成我自己的东西时,然后执行完全迁移(所以在这种情况下它意味着 C)。
我知道如何从代码执行完全迁移 :-),但是如何告诉 EF 迁移到给定级别?
从代码 (C#) 执行所有内容在这里至关重要——SQL 脚本或运行外部工具对我来说是行不通的。
对于好奇的头脑:用例——我想为中间的最后一次迁移准备测试。
我的 SQL 表有两个字段:Id 和 Status。它看起来像这样
身份证 | 地位
1 | “状态 1”
2 | “状态2”
我应该进行迁移,将这些状态值更改为我想要的状态值吗?我怎样才能做到这一点?
database-migration entity-framework-core asp.net-core entity-framework-migrations
entity-framework-migrations ×10
asp.net-core ×4
c# ×3
ef-core-2.1 ×2
.net-core ×1
primary-key ×1