EF5 Code First Automatic Migrations: Renaming Primary Key gives error: column names in each table must be unique
EF5 Code First Automatic Migrations: Renaming Primary Key gives error: column names in each table must be unique
我有一个主键 RelId
的实体 Relations
。我想将此密钥更改为 RelationId
首先,我更改了 SSMS 中的 Columnname:
SQL Table
然后我使用(在Visual Studio中)重命名将所有参考文献中的单词RelId
更改为RelationId
。
我认为 数据库和代码 中的更改会说服我的代码使用新命名的主键,但是当我 运行 项目:
column names in each table must be unique. Column name 'RelationId' in table 'dbo.Relations' is specified more than once.
当然,事实并非如此。在 table 中,列 RelationId
只存在一次。这就像 EF5 试图添加另一个具有相同名称的列。
任何人都可以解释为什么会出现此错误以及如何解决它?
Global.asax
protected void Application_Start()
{
//Database.SetInitializer<OefenfirmaContext>(new OefenfirmaDropCreateAndSeed());
Database.SetInitializer<OefenfirmaContext>(new OefenfirmaDropCreateAndSeed());
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new RazorViewEngine());
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
ModelBinders.Binders.Add(typeof(ShoppingCart), new CartModelBinder());
}
和 class OefenfirmaDropCreateAndSeed(不再播种):
public class OefenfirmaDropCreateAndSeed :
MigrateDatabaseToLatestVersion<OefenfirmaContext, Migrations.Configuration>
{
}
您使用的是 Add-Migrations 吗?然后,您无需使用手动脚本在数据库端手动更新。您应该单独在 Add-Migration 脚本中执行此操作。如果您在数据库端手动 运行 重命名列的脚本,则现有系统生成的 MigrationScripts 和数据库将不同步。因此,您将在 running/rollback new/old 迁移脚本时遇到错误。根据我的理解,请尝试以下步骤
步骤 1: 在 Visual Studio 代码中重命名模型 Class 的主键(RelId 到 RelationId)。
步骤 2:运行包管理器控制台中的 Add-Migration RenameMyColumn。 系统将根据如下模型变化生成迁移脚本。
注意:请验证系统生成的 AddColumn 和 DropColumn 而不是 RenameColumn 系统生成的迁移脚本中的如下脚本。如果是,请手动删除 AddColumn 和 DropColumn 脚本,并在 up() 和 down 中添加 RenameColumn() 脚本() 重命名主键的方法 属性.
namespace MyProject.Model.Migrations
{
using System;
using System.Data.Entity.Migrations;
public partial class RenameMyColumn : DbMigration
{
public override void Up()
{
// Remove the following auto-generated lines(Note:These scripts generated for an example purpose not for primary key)
AddColumn("dbo.MyTable", "NewColumn", c => c.String(nullable: false, maxLength: 50));
DropColumn("dbo.MyTable", "OldColumn");
// Add this line
RenameColumn("dbo.MyTable", "OldColumn", "NewColumn");
}
public override void Down()
{
// Remove the following auto-generated lines(Note:These scripts generated for an example purpose not for primary key)
AddColumn("dbo.MyTable", "OldColumn", c => c.String(nullable: false, maxLength: 50));
DropColumn("dbo.MyTable", "NewColumn");
// Add this line
RenameColumn("dbo.MyTable", "NewColumn", "OldColumn");
}
}
}
希望这可以帮助您继续前进!
我有一个主键 RelId
的实体 Relations
。我想将此密钥更改为 RelationId
首先,我更改了 SSMS 中的 Columnname: SQL Table
然后我使用(在Visual Studio中)重命名将所有参考文献中的单词RelId
更改为RelationId
。
我认为 数据库和代码 中的更改会说服我的代码使用新命名的主键,但是当我 运行 项目:
column names in each table must be unique. Column name 'RelationId' in table 'dbo.Relations' is specified more than once.
当然,事实并非如此。在 table 中,列 RelationId
只存在一次。这就像 EF5 试图添加另一个具有相同名称的列。
任何人都可以解释为什么会出现此错误以及如何解决它?
Global.asax
protected void Application_Start()
{
//Database.SetInitializer<OefenfirmaContext>(new OefenfirmaDropCreateAndSeed());
Database.SetInitializer<OefenfirmaContext>(new OefenfirmaDropCreateAndSeed());
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new RazorViewEngine());
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
ModelBinders.Binders.Add(typeof(ShoppingCart), new CartModelBinder());
}
和 class OefenfirmaDropCreateAndSeed(不再播种):
public class OefenfirmaDropCreateAndSeed :
MigrateDatabaseToLatestVersion<OefenfirmaContext, Migrations.Configuration>
{
}
您使用的是 Add-Migrations 吗?然后,您无需使用手动脚本在数据库端手动更新。您应该单独在 Add-Migration 脚本中执行此操作。如果您在数据库端手动 运行 重命名列的脚本,则现有系统生成的 MigrationScripts 和数据库将不同步。因此,您将在 running/rollback new/old 迁移脚本时遇到错误。根据我的理解,请尝试以下步骤
步骤 1: 在 Visual Studio 代码中重命名模型 Class 的主键(RelId 到 RelationId)。
步骤 2:运行包管理器控制台中的 Add-Migration RenameMyColumn。 系统将根据如下模型变化生成迁移脚本。
注意:请验证系统生成的 AddColumn 和 DropColumn 而不是 RenameColumn 系统生成的迁移脚本中的如下脚本。如果是,请手动删除 AddColumn 和 DropColumn 脚本,并在 up() 和 down 中添加 RenameColumn() 脚本() 重命名主键的方法 属性.
namespace MyProject.Model.Migrations
{
using System;
using System.Data.Entity.Migrations;
public partial class RenameMyColumn : DbMigration
{
public override void Up()
{
// Remove the following auto-generated lines(Note:These scripts generated for an example purpose not for primary key)
AddColumn("dbo.MyTable", "NewColumn", c => c.String(nullable: false, maxLength: 50));
DropColumn("dbo.MyTable", "OldColumn");
// Add this line
RenameColumn("dbo.MyTable", "OldColumn", "NewColumn");
}
public override void Down()
{
// Remove the following auto-generated lines(Note:These scripts generated for an example purpose not for primary key)
AddColumn("dbo.MyTable", "OldColumn", c => c.String(nullable: false, maxLength: 50));
DropColumn("dbo.MyTable", "NewColumn");
// Add this line
RenameColumn("dbo.MyTable", "NewColumn", "OldColumn");
}
}
}
希望这可以帮助您继续前进!