【发布时间】:2015-02-16 18:25:55
【问题描述】:
在我的 MVC 应用程序中,我使用了 Entity Framework 6 并使用代码优先的方法创建了数据库。一段时间后,我通过添加新列和删除一些列来更新其中一个实体类。为了将这些更改反映到数据库中,我遵循了以下步骤:
- 已删除项目中的迁移文件夹。
- 已删除数据库中的 __MigrationHistory 表。
然后在包管理器控制台中运行以下命令:
Enable-Migrations -EnableAutomaticMigrations -Force在配置文件中添加以下行:
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;运行:
添加迁移初始- 最后,运行:
Update-Database -Verbose
但是,我遇到错误“数据库中已经有一个名为'xxx'的对象。”
为了摆脱这个问题,我在第 5 步之后创建的初始文件中的 Up 方法中注释了代码。这可以防止错误,但数据库中没有任何更改(更新后的实体表保持不变)。 错误在哪里?提前感谢您的帮助。
这是我在 migration.cs 文件中注释的 Up 方法:
public override void Up()
{
CreateTable(
"dbo.City",
c => new
{
ID = c.Int(nullable: false, identity: true),
Name = c.String(nullable: false),
RegionID = c.Int(nullable: false),
})
.PrimaryKey(t => t.ID)
.ForeignKey("dbo.Region", t => t.RegionID)
.Index(t => t.RegionID);
CreateTable(
"dbo.Multiplier",
c => new
{
ID = c.Int(nullable: false, identity: true),
Status = c.Int(nullable: false),
Term = c.Int(nullable: false),
CityID = c.Int(nullable: false),
WhoIsOnline = c.String(nullable: false),
UserId = c.String(nullable: false),
InstituteName = c.String(nullable: false),
InstituteStatusID = c.Int(nullable: false),
InstituteAccreditationDate = c.DateTime(nullable: false),
Address = c.String(nullable: false),
Phone = c.String(nullable: false),
Fax = c.String(),
Email = c.String(nullable: false),
EurodeskEmail = c.String(nullable: false),
WebSite = c.String(),
ContactName = c.String(nullable: false),
ContactSurname = c.String(nullable: false),
ContactJobTitle = c.String(),
ContactAssignmentDate = c.DateTime(),
ContactWorkingStart = c.String(),
ContactWorkingkEnd = c.String(),
ContactPhone = c.String(),
ContactMobile = c.String(nullable: false),
ContactEmail = c.String(nullable: false),
ContactCityID = c.Int(nullable: false),
LegalRepresentativeName = c.String(nullable: false),
LegalRepresentativeSurname = c.String(nullable: false),
})
.PrimaryKey(t => t.ID)
.ForeignKey("dbo.City", t => t.CityID)
.ForeignKey("dbo.InstituteStatus", t => t.InstituteStatusID)
.Index(t => t.CityID)
.Index(t => t.InstituteStatusID);
CreateTable(
"dbo.InstituteStatus",
c => new
{
ID = c.Int(nullable: false, identity: true),
Name = c.String(nullable: false),
})
.PrimaryKey(t => t.ID);
CreateTable(
"dbo.TrainingParticipant",
c => new
{
ID = c.Int(nullable: false, identity: true),
TrainingID = c.Int(nullable: false),
ParticipantID = c.Int(nullable: false),
Multiplier_ID = c.Int(),
})
.PrimaryKey(t => t.ID)
.ForeignKey("dbo.Participant", t => t.ParticipantID)
.ForeignKey("dbo.Training", t => t.TrainingID)
.ForeignKey("dbo.Multiplier", t => t.Multiplier_ID)
.Index(t => t.TrainingID)
.Index(t => t.ParticipantID)
.Index(t => t.Multiplier_ID);
CreateTable(
"dbo.Participant",
c => new
{
ID = c.Int(nullable: false, identity: true),
Name = c.String(nullable: false),
Surname = c.String(nullable: false),
MultiplierID = c.Int(nullable: false),
})
.PrimaryKey(t => t.ID)
.ForeignKey("dbo.Multiplier", t => t.MultiplierID)
.Index(t => t.MultiplierID);
CreateTable(
"dbo.Training",
c => new
{
ID = c.Int(nullable: false, identity: true),
Name = c.String(nullable: false),
Date = c.DateTime(nullable: false),
CityID = c.Int(nullable: false),
})
.PrimaryKey(t => t.ID)
.ForeignKey("dbo.City", t => t.CityID)
.Index(t => t.CityID);
CreateTable(
"dbo.Region",
c => new
{
ID = c.Int(nullable: false, identity: true),
Name = c.String(nullable: false),
})
.PrimaryKey(t => t.ID);
}
这是migration.cs文件中的Down方法:
public override void Down()
{
DropForeignKey("dbo.City", "RegionID", "dbo.Region");
DropForeignKey("dbo.TrainingParticipant", "Multiplier_ID", "dbo.Multiplier");
DropForeignKey("dbo.TrainingParticipant", "TrainingID", "dbo.Training");
DropForeignKey("dbo.Training", "CityID", "dbo.City");
DropForeignKey("dbo.TrainingParticipant", "ParticipantID", "dbo.Participant");
DropForeignKey("dbo.Participant", "MultiplierID", "dbo.Multiplier");
DropForeignKey("dbo.Multiplier", "InstituteStatusID", "dbo.InstituteStatus");
DropForeignKey("dbo.Multiplier", "CityID", "dbo.City");
DropIndex("dbo.Training", new[] { "CityID" });
DropIndex("dbo.Participant", new[] { "MultiplierID" });
DropIndex("dbo.TrainingParticipant", new[] { "Multiplier_ID" });
DropIndex("dbo.TrainingParticipant", new[] { "ParticipantID" });
DropIndex("dbo.TrainingParticipant", new[] { "TrainingID" });
DropIndex("dbo.Multiplier", new[] { "InstituteStatusID" });
DropIndex("dbo.Multiplier", new[] { "CityID" });
DropIndex("dbo.City", new[] { "RegionID" });
DropTable("dbo.Region");
DropTable("dbo.Training");
DropTable("dbo.Participant");
DropTable("dbo.TrainingParticipant");
DropTable("dbo.InstituteStatus");
DropTable("dbo.Multiplier");
DropTable("dbo.City");
}
【问题讨论】:
-
这对我很有帮助,因为当更新命令失败时,我的 Oracle 数据库似乎删除了 __MigrationHistory。我收到一个关于无法处理 OracleException 的异常,所以我不得不重置 很多。 (我仍然不知道为什么 Oracle 不处理异常)
标签: sql-server asp.net-mvc entity-framework ef-code-first updatemodel