In my MVC application I used Entity Framework 6 and created database with code first approach. After a certain time, I updated one of the entity classes by adding new column and removing some columns. For reflecting these changes to the database I followed the steps below:
- Deleted the migrations folder in the project.
- Deleted the __MigrationHistory table in the database.
Then run the following command in the Package Manager Console:
Enable-Migrations -EnableAutomaticMigrations -ForceAdd the following lines in configuration file:
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;Run:
Add-Migration Initial- And finally, run:
Update-Database -Verbose
However, I encounter an error "There is already an object named 'xxx' in the database."
To get rid of this problem, I comment the code in the Up method in the initial file created after 5th step. This prevent the error but nothing is changed in the database (the updated entity tables remains as before). Where is the mistake? Thanks in advance for your help.
Here is the Up method that I commented in the migration.cs file:
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);
}
And this is the Down method in the migration.cs file:
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");
}