从int PK更改为字符串PK

时间:2017-03-14 11:42:06

标签: c# entity-framework asp.net-mvc-5

在我的应用程序中,我的models int Id primary keymodels。现在我已经到了我真正需要三个string IdId的地步。我将所有三个string更改为migration,但在创建update-database并运行public class Project { public int Id { get; set; } [ForeignKey("VendorId")] public Vendor Vendor { get; set; } public string VendorId { get; set; } [ForeignKey("ClientId")] public Client Client { get; set; } public string ClientId { get; set; } [ForeignKey("AdministratorId")] public Administrator Administrator { get; set; } [Display(Name = "Administrator")] public string AdministratorId { get; set; } } 后,我收到错误消息。这就是我现在拥有的: 项目模型:

public class Client
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public string Id { get; set; }
}

客户端模型:

public class Vendor
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public string Id { get; set; }
}

供应商模型:

public class Administrator
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public string Id { get; set; }
}

管理员模型:

Vendor

(提到我删除了不必要的代码) 当ClientAdministratorint Id模型具有update-database时,这非常有效,但现在,这是我在System.Data.SqlClient.SqlException (0x80131904): The constraint 'PK_dbo.Administrators' is being referenced by table 'Projects', foreign key constraint 'FK_dbo.Jobs_dbo.Administrators_AdministratorId'. Could not drop constraint. See previous errors. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<NonQuery>b__0(DbCommand t, DbCommandInterceptionContext`1 c) at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed) at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand command, DbCommandInterceptionContext interceptionContext) at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteNonQuery() at System.Data.Entity.Migrations.DbMigrator.ExecuteSql(MigrationStatement migrationStatement, DbConnection connection, DbTransaction transaction, DbInterceptionContext interceptionContext) at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ExecuteSql(MigrationStatement migrationStatement, DbConnection connection, DbTransaction transaction, DbInterceptionContext interceptionContext) at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable`1 migrationStatements, DbConnection connection, DbTransaction transaction, DbInterceptionContext interceptionContext) at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsWithinTransaction(IEnumerable`1 migrationStatements, DbTransaction transaction, DbInterceptionContext interceptionContext) at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsWithinNewTransaction(IEnumerable`1 migrationStatements, DbConnection connection, DbInterceptionContext interceptionContext) at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable`1 migrationStatements, DbConnection connection, DbInterceptionContext interceptionContext) at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable`1 migrationStatements, DbConnection connection) at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClass30.<ExecuteStatements>b__2e() at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.<Execute>b__0() at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation) at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation) at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements, DbTransaction existingTransaction) at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable`1 migrationStatements) at System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, VersionedModel targetModel, IEnumerable`1 operations, IEnumerable`1 systemOperations, Boolean downgrading, Boolean auto) at System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration migration, DbMigration lastMigration) at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ApplyMigration(DbMigration migration, DbMigration lastMigration) at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId) at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId) at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration) at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClassc.<Update>b__b() at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration) at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run() at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate) at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate) at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner) at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force) at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0() at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command) ClientConnectionId:b67d4f49-f332-4897-a129-f10751ee40ed Error Number:3725,State:0,Class:16 The constraint 'PK_dbo.Administrators' is being referenced by table 'Projects', foreign key constraint 'FK_dbo.Jobs_dbo.Administrators_AdministratorId'. Could not drop constraint. See previous errors. 命令上得到的错误:

public override void Up()
{
    DropForeignKey("dbo.Projects","ClientId","dbo.Clients");
    DropForeignKey("dbo.InvoicePayables","AdministratorId","dbo.Administrators");
    DropForeignKey("dbo.InvoicePayables","VendorId","dbo.Vendors");
    DropForeignKey("dbo.InvoiceTakings","AdministratorId","dbo.Administrators");
    DropForeignKey("dbo.InvoiceTakings","ClientId","dbo.Clients");
    DropForeignKey("dbo.Projects","AdministratorId","dbo.Administrators");
    DropForeignKey("dbo.ClientContactPersons","Client_Id","dbo.Clients");
    DropForeignKey("dbo.Translations","Vendor_Id","dbo.Vendors");
    DropForeignKey("dbo.Projects","VendorId","dbo.Vendors");
    DropIndex("dbo.ClientContactPersons",new[]{"Client_Id"});
    DropIndex("dbo.Projects",new[]{"VendorId"});
    DropIndex("dbo.Projects",new[]{"ClientId"});
    DropIndex("dbo.Projects",new[]{"AdministratorId"});
    DropIndex("dbo.InvoicePayables",new[]{"VendorId"});
    DropIndex("dbo.InvoicePayables",new[]{"AdministratorId"});
    DropIndex("dbo.Translations",new[]{"Vendor_Id"});
    DropIndex("dbo.InvoiceTakings",new[]{"ClientId"});
    DropIndex("dbo.InvoiceTakings",new[]{"AdministratorId"});
    DropPrimaryKey("dbo.Administrators");
    DropPrimaryKey("dbo.Clients");
    DropPrimaryKey("dbo.Vendors");
    AddColumn("dbo.InvoiceTakings","Administrator_Id",c=>c.String(maxLength:128));
    AddColumn("dbo.InvoiceTakings","Client_Id",c=>c.String(maxLength:128));
    AlterColumn("dbo.Administrators","Id",c=>c.String(nullable:false,maxLength:128));
    AlterColumn("dbo.ClientContactPersons","Client_Id",c=>c.String(maxLength:128));
    AlterColumn("dbo.Clients","Id",c=>c.String(nullable:false,maxLength:128));
    AlterColumn("dbo.Projects","VendorId",c=>c.String(maxLength:128));
    AlterColumn("dbo.Projects","ClientId",c=>c.String(maxLength:128));
    AlterColumn("dbo.Projects","AdministratorId",c=>c.String(maxLength:128));
    AlterColumn("dbo.InvoicePayables","VendorId",c=>c.String(maxLength:128));
    AlterColumn("dbo.InvoicePayables","AdministratorId",c=>c.String(maxLength:128));
    AlterColumn("dbo.Vendors","Id",c=>c.String(nullable:false,maxLength:128));
    AlterColumn("dbo.Translations","Vendor_Id",c=>c.String(maxLength:128));
    AddPrimaryKey("dbo.Administrators","Id");
    AddPrimaryKey("dbo.Clients","Id");
    AddPrimaryKey("dbo.Vendors","Id");
    CreateIndex("dbo.ClientContactPersons","Client_Id");
    CreateIndex("dbo.Projects","VendorId");
    CreateIndex("dbo.Projects","ClientId");
    CreateIndex("dbo.Projects","AdministratorId");
    CreateIndex("dbo.InvoicePayables","VendorId");
    CreateIndex("dbo.InvoicePayables","AdministratorId");
    CreateIndex("dbo.Translations","Vendor_Id");
    CreateIndex("dbo.InvoiceTakings","Administrator_Id");
    CreateIndex("dbo.InvoiceTakings","Client_Id");
    AddForeignKey("dbo.Projects","ClientId","dbo.Clients","Id");
    AddForeignKey("dbo.InvoicePayables","AdministratorId","dbo.Administrators","Id");
    AddForeignKey("dbo.InvoicePayables","VendorId","dbo.Vendors","Id");
    AddForeignKey("dbo.InvoiceTakings","Administrator_Id","dbo.Administrators","Id");
    AddForeignKey("dbo.InvoiceTakings","Client_Id","dbo.Clients","Id");
    AddForeignKey("dbo.Projects","AdministratorId","dbo.Administrators","Id");
    AddForeignKey("dbo.ClientContactPersons","Client_Id","dbo.Clients","Id");
    AddForeignKey("dbo.Translations","Vendor_Id","dbo.Vendors","Id");
    AddForeignKey("dbo.Projects","VendorId","dbo.Vendors","Id");
}

public override void Down()
{
    DropForeignKey("dbo.Projects","VendorId","dbo.Vendors");
    DropForeignKey("dbo.Translations","Vendor_Id","dbo.Vendors");
    DropForeignKey("dbo.ClientContactPersons","Client_Id","dbo.Clients");
    DropForeignKey("dbo.Projects","AdministratorId","dbo.Administrators");
    DropForeignKey("dbo.InvoiceTakings","Client_Id","dbo.Clients");
    DropForeignKey("dbo.InvoiceTakings","Administrator_Id","dbo.Administrators");
    DropForeignKey("dbo.InvoicePayables","VendorId","dbo.Vendors");
    DropForeignKey("dbo.InvoicePayables","AdministratorId","dbo.Administrators");
    DropForeignKey("dbo.Projects","ClientId","dbo.Clients");
    DropIndex("dbo.InvoiceTakings",new[]{"Client_Id"});
    DropIndex("dbo.InvoiceTakings",new[]{"Administrator_Id"});
    DropIndex("dbo.Translations",new[]{"Vendor_Id"});
    DropIndex("dbo.InvoicePayables",new[]{"AdministratorId"});
    DropIndex("dbo.InvoicePayables",new[]{"VendorId"});
    DropIndex("dbo.Projects",new[]{"AdministratorId"});
    DropIndex("dbo.Projects",new[]{"ClientId"});
    DropIndex("dbo.Projects",new[]{"VendorId"});
    DropIndex("dbo.ClientContactPersons",new[]{"Client_Id"});
    DropPrimaryKey("dbo.Vendors");
    DropPrimaryKey("dbo.Clients");
    DropPrimaryKey("dbo.Administrators");
    AlterColumn("dbo.Translations","Vendor_Id",c=>c.Int());
    AlterColumn("dbo.Vendors","Id",c=>c.Int(nullable:false,identity:true));
    AlterColumn("dbo.InvoicePayables","AdministratorId",c=>c.Int(nullable:false));
    AlterColumn("dbo.InvoicePayables","VendorId",c=>c.Int(nullable:false));
    AlterColumn("dbo.Projects","AdministratorId",c=>c.Int());
    AlterColumn("dbo.Projects","ClientId",c=>c.Int(nullable:false));
    AlterColumn("dbo.Projects","VendorId",c=>c.Int());
    AlterColumn("dbo.Clients","Id",c=>c.Int(nullable:false,identity:true));
    AlterColumn("dbo.ClientContactPersons","Client_Id",c=>c.Int());
    AlterColumn("dbo.Administrators","Id",c=>c.Int(nullable:false,identity:true));
    DropColumn("dbo.InvoiceTakings","Client_Id");
    DropColumn("dbo.InvoiceTakings","Administrator_Id");
    AddPrimaryKey("dbo.Vendors","Id");
    AddPrimaryKey("dbo.Clients","Id");
    AddPrimaryKey("dbo.Administrators","Id");
    CreateIndex("dbo.InvoiceTakings","AdministratorId");
    CreateIndex("dbo.InvoiceTakings","ClientId");
    CreateIndex("dbo.Translations","Vendor_Id");
    CreateIndex("dbo.InvoicePayables","AdministratorId");
    CreateIndex("dbo.InvoicePayables","VendorId");
    CreateIndex("dbo.Projects","AdministratorId");
    CreateIndex("dbo.Projects","ClientId");
    CreateIndex("dbo.Projects","VendorId");
    CreateIndex("dbo.ClientContactPersons","Client_Id");
    AddForeignKey("dbo.Projects","VendorId","dbo.Vendors","Id");
    AddForeignKey("dbo.Translations","Vendor_Id","dbo.Vendors","Id");
    AddForeignKey("dbo.ClientContactPersons","Client_Id","dbo.Clients","Id");
    AddForeignKey("dbo.Projects","AdministratorId","dbo.Administrators","Id");
    AddForeignKey("dbo.InvoiceTakings","ClientId","dbo.Clients","Id",cascadeDelete:true);
    AddForeignKey("dbo.InvoiceTakings","AdministratorId","dbo.Administrators","Id",cascadeDelete:true);
    AddForeignKey("dbo.InvoicePayables","VendorId","dbo.Vendors","Id",cascadeDelete:true);
    AddForeignKey("dbo.InvoicePayables","AdministratorId","dbo.Administrators","Id",cascadeDelete:true);
    AddForeignKey("dbo.Projects","ClientId","dbo.Clients","Id",cascadeDelete:true);
}

更新迁移代码:

{{1}}

(很抱歉,迁移的长代码...包括所有其他属性) 我错过了什么?

0 个答案:

没有答案