C# 已经有一个名为';员工';在数据库中
我尝试更新数据库,但出现以下错误: 数据库中已存在名为“Employee”的对象 这是我的最后一个迁移文件:C# 已经有一个名为';员工';在数据库中,c#,.net,asp.net-mvc,entity-framework,C#,.net,Asp.net Mvc,Entity Framework,我尝试更新数据库,但出现以下错误: 数据库中已存在名为“Employee”的对象 这是我的最后一个迁移文件: public partial class explain : DbMigration { public override void Up() { DropForeignKey("dbo.Employee", "Person_PersonId", "dbo.Person"); DropForeignK
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; }
}
堆栈跟踪:
[SqlException(0x80131904):已经有一个名为
数据库中的“雇员”。]System.Data.SqlClient.SqlConnection.OnError(SqlException异常, 布尔断开连接,操作
1 wrapCloseInAction)+2418102
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException
异常,布尔断开连接,操作
1 wrapCloseInAction)
+5694456 System.Data.SqlClient.TdsParser.ThroweException和Warning(TdsParserStateObject
stateObj,布尔调用方连接锁,布尔异步关闭)+285System.Data.SqlClient.TdsParser.TryRun(运行行为, SqlCommand cmdHandler、SqlDataReader数据流、, BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj、Boolean和dataReady)+3731
System.Data.SqlClient.SqlCommand.RunExecuteOnQueryTds(字符串 methodName,布尔异步,Int32超时,布尔异步写入)+959
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource
1
完成,字符串方法名,布尔sendToPipe,Int32超时,
布尔异步写入)+272
System.Data.SqlClient.SqlCommand.ExecuteNonQuery()+280
System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.b__0(DbCommand
t、 DbCommandInterceptionContext
1c)+36System.Data.Entity.Infrastructure.InternalDispatcher
1.Dispatch(TTarget
目标,函数
3操作,TInterceptionContext拦截context,
动作3执行,动作3执行)+138
System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand
命令,dbcommandinterceptoncontext interceptoncontext)+476
System.Data.Entity.Internal.InterceptableDbCommand.ExecuteOnQuery()
+177 System.Data.Entity.Migrations.Dbmigator.ExecuteSql(MigrationStatement
迁移语句、数据库连接、数据库事务
事务,dbinterceptoncontext interceptoncontext)+194
System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteSql(MigrationStatement
迁移语句、数据库连接、数据库事务
事务,dbinterceptoncontext interceptoncontext)+62
System.Data.Entity.Migrations.Dbmigator.ExecuteStatementInternal(IEnumerable1
迁移语句、数据库连接、数据库事务
事务,dbinterceptoncontext interceptoncontext)+113
System.Data.Entity.Migrations.Dbmigator.ExecuteStatementSwithintTransaction(IEnumerable
1
迁移语句、DbTransaction事务、DbInterceptionContext
拦截上下文)+110
System.Data.Entity.Migrations.Dbmigator.ExecuteStatementSwitthingTransaction(IEnumerable1
迁移语句、数据库连接、数据库拦截上下文
拦截上下文)+172
System.Data.Entity.Migrations.Dbmigator.ExecuteStatementInternal(IEnumerable
1
迁移语句、数据库连接、数据库拦截上下文
拦截上下文)+429
System.Data.Entity.Migrations.Dbmigator.ExecuteStatementInternal(IEnumerable1
迁移语句,数据库连接)+646
System.Data.Entity.Migrations.c__DisplayClass30.b__2e()
+66 System.Data.Entity.SqlServer.c__DisplayClass1.b__0()+34 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Func
1
操作)+251
System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(操作
操作)+196
System.Data.Entity.Migrations.Dbmigator.ExecuteStatements(IEnumerable1
迁移语句,DbTransaction existingTransaction)+327
System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable
1
迁移状态)+39
System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable1
迁移状态)+42
System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(字符串
migrationId,VersionedModel targetModel,IEnumerable
1操作,
IEnumerable1系统操作、布尔降级、布尔自动)
+1372 System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration
迁移,DbMigration lastMigration)+783
System.Data.Entity.Migrations.Infrastructure.MigratorBase.ApplyMigration(DbMigration
迁移,DbMigration lastMigration)+56
System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable
1
pendingMigrations,字符串targetMigrationId,字符串lastMigrationId)
+192 System.Data.Entity.Migrations.Infrastructure.MigratorBase.Upgrade(IEnumerable1
pendingMigrations,字符串targetMigrationId,字符串lastMigrationId)
+59 System.Data.Entity.Migrations.Dbmigator.UpdateInternal(字符串targetMigration)+888
System.Data.Entity.Migrations.c__DisplayClassc.b__b()+38
System.Data.Entity.Migrations.Dbmigator.EnsureDatabaseExists(操作
必须成功访问数据库)+516
System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(操作
必须成功访问数据库)+42
System.Data.Entity.Migrations.DbMigrator.Update(字符串
targetMigration)+136
System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update()+33
System.Data.Entity.Internal.DatabaseCreator.CreateDatabase(InternalContext
internalContext,Func
3 createMigrator,ObjectContext)
+175 System.Data.Entity.Internal.InternalContext.CreateDatabase(ObjectContext
objectContext,DatabaseExistenceEstate)+150
System.Data.Entity.Database.Create(DatabaseExistenceEstate
ExistenceEstate)+444
System.Data.Entity.DropCreateDatabaseAlways1.InitializeDatabase(TContext
上下文)+158
System.Data.Entity.Internal.c_显示
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;
}
}
}