[英]There is already an object named '__MigrationHistory' in the database
[英]There is already an object named 'Employee' in the database
我尝试更新数据库,但出现此错误:
数据库中已经有一个名为“ Employee”的对象。
这是我的上一个迁移文件:
public partial class explain : DbMigration
{
public override void Up()
{
DropForeignKey("dbo.Employee", "Person_PersonId", "dbo.Person");
DropForeignKey("dbo.Receptionist", "PersonId", "dbo.Employee");
DropIndex("dbo.Employee", new[] { "Person_PersonId" });
CreateTable(
"dbo.Employee",
c => new
{
PersonId = c.Int(nullable: false),
Receptionist_PersonId = c.Int(),
Salary = c.Int(nullable: false),
Hiredate = c.DateTime(nullable: false),
isManager = c.Boolean(nullable: false),
})
.PrimaryKey(t => t.PersonId)
.ForeignKey("dbo.Person", t => t.PersonId)
.ForeignKey("dbo.Receptionist", t => t.Receptionist_PersonId)
.Index(t => t.PersonId)
.Index(t => t.Receptionist_PersonId);
AddForeignKey("dbo.Receptionist", "PersonId", "dbo.Employee", "PersonId");
DropTable("dbo.Employee");
}
public override void Down()
{
CreateTable(
"dbo.Employee",
c => new
{
PersonId = c.Int(nullable: false),
Salary = c.Int(nullable: false),
Hiredate = c.DateTime(nullable: false),
isManager = c.Boolean(nullable: false),
Person_PersonId = c.Int(nullable: false),
})
.PrimaryKey(t => t.PersonId);
DropForeignKey("dbo.Employee", "Receptionist_PersonId", "dbo.Receptionist");
DropForeignKey("dbo.Receptionist", "PersonId", "dbo.Employee");
DropForeignKey("dbo.Employee", "PersonId", "dbo.Person");
DropIndex("dbo.Employee", new[] { "Receptionist_PersonId" });
DropIndex("dbo.Employee", new[] { "PersonId" });
DropTable("dbo.Employee");
CreateIndex("dbo.Employee", "Person_PersonId");
AddForeignKey("dbo.Receptionist", "PersonId", "dbo.Employee", "PersonId");
AddForeignKey("dbo.Employee", "Person_PersonId", "dbo.Person", "PersonId");
}
}
Employee.cs模型
public class Employee : Person
{
public Employee()
{
EmployeeSchedule = new HashSet<EmployeeSchedule>();
Vacation = new HashSet<Vacation>();
}
[Required]
[Display(Name = "Wynagrodzenie")]
public int Salary { get; set; }
[Required]
[Display(Name = "Data zatrudnienia")]
public DateTime Hiredate { get; set; }
[Display(Name = "Kierownik")]
public bool isManager { get; set; }
public virtual ICollection<EmployeeSchedule> EmployeeSchedule { get; set; }
public virtual Lifeguard Lifeguard { get; set; }
public virtual Receptionist Receptionist { get; set; }
public virtual Trainer Trainer { get; set; }
public virtual ICollection<Vacation> Vacation { get; set; }
}
人.cs
public abstract class Person
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int PersonId { get; set; }
[Required]
public long Pesel { get; set; }
[Required]
[StringLength(30, ErrorMessage = "Imię nie może zawierać więcej niż 30 znaków.")]
[Display(Name = "Imię")]
public string FirstName { get; set; }
[Required]
[StringLength(60, ErrorMessage = "Nazwisko nie może zawierać wiecej niż 60 znaków.")]
[Display(Name = "Nazwisko")]
public string LastName { get; set; }
[Required]
[EmailAddress(ErrorMessage = "Błędny format adresu e-mail.")]
public string Email { get; set; }
[Required]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
[Display(Name = "Data urodzenia")]
public DateTime DateOfBirth { get; set; }
[NotMapped]
public int age;
[NotMapped]
public int Age
{
get
{
DateTime today = DateTime.Today;
age = today.Year - DateOfBirth.Year;
if (DateOfBirth > today.AddYears(-age))
{
age--;
}
return age;
}
}
[NotMapped]
[Display(Name="Imię i Nazwisko")]
public string FullName
{
get
{
return FirstName + " " + LastName;
}
}
}
堆栈跟踪:
[SqlException(0x80131904):数据库中已经有一个名为“ Employee”的对象。
System.Data.SqlClient.SqlConnection.OnError(SqlException异常,布尔值breakConnection,操作1 wrapCloseInAction) +2418102
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) +2418102
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) +2418102
1 wrapCloseInAction)+5694456 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,布尔值调用程序HasConnectionLock,布尔值asyncClose)+285
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action
System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj,Boolean&dataReady)+3731
System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(字符串methodName,布尔异步,Int32超时,布尔asyncWrite)+959
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) +272
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +280
System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<NonQuery>b__0(DbCommand t, DbCommandInterceptionContext1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) +272
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +280
System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<NonQuery>b__0(DbCommand t, DbCommandInterceptionContext1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) +272
1 c)+36
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +280
System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<NonQuery>b__0(DbCommand t, DbCommandInterceptionContext
System.Data.Entity.Infrastructure.Interception.InternalDispatcher1.Dispatch(TTarget target, Func
3操作,TInterceptionContext拦截上下文,3 executing, Action
3,执行3 executing, Action
3)+138
System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand命令,DbCommandInterceptionContext拦截上下文)+476
System.Data.Entity.Internal.InterceptableDbCommand.ExecuteNonQuery()+177 System.Data.Entity.Migrations.DbMigrator.ExecuteSql(MigrationStatement migrationStatement,DbConnection连接,DbTransaction事务,DbInterceptionContext拦截上下文)+194
System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteSql(MigrationStatement migrationStatement,DbConnection连接,DbTransaction事务,DbInterceptionContext拦截上下文)+62
System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable1 migrationStatements, DbConnection connection, DbTransaction transaction, DbInterceptionContext interceptionContext) +113
System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsWithinTransaction(IEnumerable1 migrationStatements, DbConnection connection, DbTransaction transaction, DbInterceptionContext interceptionContext) +113
System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsWithinTransaction(IEnumerable1 migrationStatements, DbConnection connection, DbTransaction transaction, DbInterceptionContext interceptionContext) +113
1 migrationStatements,DbTransaction事务,DbInterceptionContext拦截上下文)+110
System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsWithinTransaction(IEnumerable
System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsWithinNewTransaction(IEnumerable1 migrationStatements, DbConnection connection, DbInterceptionContext interceptionContext) +172
System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable1 migrationStatements, DbConnection connection, DbInterceptionContext interceptionContext) +172
System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable1 migrationStatements, DbConnection connection, DbInterceptionContext interceptionContext) +172
1 migrationStatements,DbConnection连接,DbInterceptionContext拦截上下文)+429
System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable
System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable1 migrationStatements, DbConnection connection) +646
System.Data.Entity.Migrations.<>c__DisplayClass30.<ExecuteStatements>b__2e() +66 System.Data.Entity.SqlServer.<>c__DisplayClass1.<Execute>b__0() +34 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Func1 migrationStatements, DbConnection connection) +646
System.Data.Entity.Migrations.<>c__DisplayClass30.<ExecuteStatements>b__2e() +66 System.Data.Entity.SqlServer.<>c__DisplayClass1.<Execute>b__0() +34 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Func1 migrationStatements, DbConnection connection) +646
1操作)+251
System.Data.Entity.Migrations.<>c__DisplayClass30.<ExecuteStatements>b__2e() +66 System.Data.Entity.SqlServer.<>c__DisplayClass1.<Execute>b__0() +34 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Func
System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(操作操作)+196
System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable1 migrationStatements, DbTransaction existingTransaction) +327
System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable1 migrationStatements, DbTransaction existingTransaction) +327
System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable1 migrationStatements, DbTransaction existingTransaction) +327
1 migrationStatements)+39
System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable
System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable1 migrationStatements) +42
System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, VersionedModel targetModel, IEnumerable1 migrationStatements) +42
System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, VersionedModel targetModel, IEnumerable1 migrationStatements) +42
1操作,IEnumerable
System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, VersionedModel targetModel, IEnumerable1 systemOperations, Boolean downgrading, Boolean auto) +1372 System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration migration, DbMigration lastMigration) +783
System.Data.Entity.Migrations.Infrastructure.MigratorBase.ApplyMigration(DbMigration migration, DbMigration lastMigration) +56
System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable1 systemOperations, Boolean downgrading, Boolean auto) +1372 System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration migration, DbMigration lastMigration) +783
System.Data.Entity.Migrations.Infrastructure.MigratorBase.ApplyMigration(DbMigration migration, DbMigration lastMigration) +56
System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable1 systemOperations, Boolean downgrading, Boolean auto) +1372 System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration migration, DbMigration lastMigration) +783
1endingMigrations,字符串targetMigrationId,字符串lastMigrationId)+192 System.Data.Entity.Migrations.Infrastructure.MigratorBase.Upgrade(IEnumerable
System.Data.Entity.Migrations.Infrastructure.MigratorBase.ApplyMigration(DbMigration migration, DbMigration lastMigration) +56
System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable1 pendingMigrations, String targetMigrationId, String lastMigrationId) +59 System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration) +888
System.Data.Entity.Migrations.<>c__DisplayClassc.<Update>b__b() +38
System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) +516
System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) +42
System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) +136
System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update() +33 System.Data.Entity.Internal.DatabaseCreator.CreateDatabase(InternalContext internalContext, Func1 pendingMigrations, String targetMigrationId, String lastMigrationId) +59 System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration) +888
System.Data.Entity.Migrations.<>c__DisplayClassc.<Update>b__b() +38
System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) +516
System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) +42
System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) +136
System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update() +33 System.Data.Entity.Internal.DatabaseCreator.CreateDatabase(InternalContext internalContext, Func1 pendingMigrations, String targetMigrationId, String lastMigrationId) +59 System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration) +888
3 createMigrator,ObjectContext objectContext)+175 System.Data.Entity.Internal.InternalContext .CreateDatabase(ObjectContext objectContext,DatabaseExistenceState existState)+150
System.Data.Entity.Migrations.<>c__DisplayClassc.<Update>b__b() +38
System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) +516
System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) +42
System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) +136
System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update() +33 System.Data.Entity.Internal.DatabaseCreator.CreateDatabase(InternalContext internalContext, Func
System.Data.Entity.Database.Create(DatabaseExistenceState existState)+444
System.Data.Entity.DropCreateDatabaseAlways1.InitializeDatabase(TContext context) +158
System.Data.Entity.Internal.<>c__DisplayClassf1.InitializeDatabase(TContext context) +158
System.Data.Entity.Internal.<>c__DisplayClassf1.InitializeDatabase(TContext context) +158
1.b__e()+165 System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action操作)+110
System.Data.Entity.Internal.<>c__DisplayClassf
System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()+660 System.Data.Entity.Internal.LazyInternalContext.b__4(InternalContext c)+31
System.Data.Entity.Internal.RetryAction1.PerformAction(TInput input) +143 System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action
1动作)+292
System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()+123 System.Data.Entity.Internal.InternalContext.Initialize()+42 System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)+39
System.Data.Entity.Internal.Linq.InternalSet1.Initialize() +137
System.Data.Entity.Internal.Linq.InternalSet1.Initialize() +137
System.Data.Entity.Internal.Linq.InternalSet1.Initialize() +137
1.get_InternalContext()+38 System.Data.Entity.Internal.Linq.InternalSet
System.Data.Entity.Internal.Linq.InternalSet1.FindAsync(CancellationToken cancellationToken, Object[] keyValues) +58
System.Data.Entity.DbSet1.FindAsync(CancellationToken cancellationToken, Object[] keyValues) +58
System.Data.Entity.DbSet1.FindAsync(CancellationToken cancellationToken, Object[] keyValues) +58
1.FindAsync(CancellationToken cancelleToken,Object [] keyValues)+70
System.Data.Entity.DbSet
System.Data.Entity.DbSet1.FindAsync(Object[] keyValues) +69
Microsoft.AspNet.Identity.EntityFramework.EntityStore1.FindAsync(Object[] keyValues) +69
Microsoft.AspNet.Identity.EntityFramework.EntityStore1.FindAsync(Object[] keyValues) +69
1.GetByIdAsync(Object id)+104
Microsoft.AspNet.Identity.EntityFramework.EntityStore
Microsoft.AspNet.Identity.EntityFramework.d__6c.MoveNext()+275 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)+99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)+58 System.Runtime.CompilerServices.TaskAwaiter1.GetResult() +28 Microsoft.AspNet.Identity.CultureAwaiter
1.GetResult()1.GetResult() +28 Microsoft.AspNet.Identity.CultureAwaiter
123 Microsoft.AspNet.Identity.Owin 。<b__1> d__4.MoveNext()+1519 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)+99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)+58 System.Runtime.CompilerServices.TaskAwaiter.GetResult()+26 Microsoft.Owin.Security.Cookies.d__2.MoveNext()+3729 System.Runtime.CompilerServices.TaskAwaiter .ThrowForNonSuccess(任务任务)+99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)+58 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()+28 Microsoft.Owin.Security.Infrastructure.d__0.MoveNext()+810 System.Runtime.CompilerServices .TaskAwaiter.ThrowForNonSuccess(任务任务)+99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)+58 System.Runtime.CompilerServices.TaskAwaiter.GetResult()+26 Microsoft.Owin.Security.Infrastructure.d__0.MoveNext()+427 System.Runtime.CompilerServices.TaskAwaiter .ThrowForNonSuccess(任务任务)+99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)+58 System.Runtime.CompilerServices.TaskAwaiter.GetResult()+26 Microsoft.AspNet.Identity.Owin.d__0.MoveNext()+641 System.Runtime.CompilerServices.TaskAwaiter。 .ThrowForNonSuccess(任务任务)+99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)+58 System.Runtime.CompilerServices.TaskAwaiter.GetResult()+26 Microsoft.AspNet.Identity.Owin.d__0.MoveNext()+641 System.Runtime.CompilerServices.TaskAwaiter。 .ThrowForNonSuccess(任务任务)+99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)+58 System.Runtime.CompilerServices.TaskAwaiter.GetResult()+26 Microsoft.AspNet.Identity.Owin.d__0.MoveNext()+641 System.Runtime.CompilerServices.TaskAwaiter。 .ThrowForNonSuccess(任务任务)+99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)+58 System.Runtime.CompilerServices.TaskAwaiter.GetResult()+26 Microsoft.AspNet.Identity.Owin.d__0.MoveNext()+641 System.Runtime.CompilerServices.TaskAwaiter。 .ThrowForNonSuccess(任务任务)+99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)+58 System.Runtime.CompilerServices.TaskAwaiter.GetResult()+26 Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.d__5.MoveNext()+287 System.Runtime.CompilerServices .TaskAwaiter.ThrowForNonSuccess(任务任务)+99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)+58 System.Runtime.CompilerServices.TaskAwaiter.GetResult()+26 Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.d__2.MoveNext()+272 System.Runtime.ExceptionServices .ExceptionDispatchInfo.Throw()+26 Microsoft.Owin.Host.SystemWeb.Infrastructure.ErrorState.Rethrow()+33 Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult.End(IAsyncResult ar)+150
Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.EndFinalWork(IAsyncResult ar)+42
System.Web.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+380 System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔值并已完成)+155
尝试在迁移中在创建语句之前将DropTable语句上移。 这样,您可以在创建新员工表之前删除现有员工表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.