0

I've been through answers on this question here and so far nothing mention applies or has fixed the issue.

Edit

What I've tried:

  • Updated ContextKey column in __MigrationHistory table.
  • Copied all my older migrations back from the old version of my project.

Problem

I've just come across an error in my app that was caused by 0 values in a column that's supposed to link 2 tables. As such, I've updated my data model to structure a relationship between them:

public class ContactGroup
{
    public int Id { get; set; }
    public string Name { get; set; }
}
public class Contact
{
    public int Id { get; set; }
    public ContactGroup ContactGroup { get; set; }
    // other contact columns go here
}

One of the other tables I have is called Campaigns, so now having added a migration that sets up my foreign keys, I want to update-database

Here's where my problem is since I'm getting an error saying:

There is already an object named 'Campaigns' in the database.

I know there's already an object with this name in the database because its one of the objects I'm establishing a relationship to.

Here's the Migration as generated by Entity Framework

public partial class StructuredRelationships : DbMigration
{
    public override void Up()
    {
        CreateTable(
            "dbo.Campaigns",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    Name = c.String(),
                    Client = c.String(),
                    EmailAddress = c.String(),
                    PhoneNumber = c.String(),
                    FaxNumber = c.String(),
                    PhysicalAddress = c.String(),
                    PostalAddress = c.String(),
                    DateSent = c.DateTime(),
                    SmtpServerAddress = c.String(),
                    SmtpServerPort = c.Int(nullable: false),
                    SmtpServerUser = c.String(),
                    SmtpServerPassword = c.String(),
                    Deleted = c.Boolean(nullable: false),
                    SendInterval = c.Int(nullable: false),
                })
            .PrimaryKey(t => t.Id);
        
        CreateTable(
            "dbo.ContactGroups",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    Name = c.String(),
                    Deleted = c.Boolean(nullable: false),
                })
            .PrimaryKey(t => t.Id);
        
        CreateTable(
            "dbo.Contacts",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    ContactGroupId = c.Int(nullable: false),
                    CompanyName = c.String(),
                    Salutation = c.String(),
                    FirstName = c.String(),
                    LastName = c.String(),
                    EmailAddress = c.String(),
                    Telephone = c.String(),
                    DirectTel = c.String(),
                    Mobile = c.String(),
                    BroadDesignation = c.String(),
                    Designation = c.String(),
                    EmployeeCount = c.Int(nullable: false),
                    EmployeeCountBand = c.String(),
                    SaTelProvince = c.String(),
                    SaBroadArea = c.String(),
                    Town = c.String(),
                    Suburb = c.String(),
                    Type = c.String(),
                    SpecificBusinessClassification = c.String(),
                    ReferenceNumber = c.String(),
                    Deleted = c.Boolean(nullable: false),
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.ContactGroups", t => t.ContactGroupId, cascadeDelete: true)
            .Index(t => t.ContactGroupId);
        
        CreateTable(
            "dbo.Emails",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    CampaignId = c.Int(nullable: false),
                    TemplateId = c.Int(nullable: false),
                    Sender = c.String(),
                    From = c.String(),
                    FromAddress = c.String(),
                    ReplyTo = c.String(),
                    ReplyToAddress = c.String(),
                    Subject = c.String(),
                    Body = c.String(),
                    Footer = c.String(),
                    DateCreated = c.DateTime(nullable: false),
                    Deleted = c.Boolean(nullable: false),
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.Campaigns", t => t.CampaignId, cascadeDelete: true)
            .Index(t => t.CampaignId);
        
        CreateTable(
            "dbo.EmailSends",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    CampaignId = c.Int(nullable: false),
                    EmailId = c.Int(nullable: false),
                    ContactGroupId = c.Int(nullable: false),
                    SendDate = c.DateTime(nullable: false),
                    TotalRecipients = c.Int(nullable: false),
                    TotalSent = c.Int(nullable: false),
                    TotalFailed = c.Int(nullable: false),
                    Status = c.String(),
                    CompletedDate = c.DateTime(),
                    Deleted = c.Boolean(nullable: false),
                    TrackerUrl = c.String(),
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.Campaigns", t => t.CampaignId, cascadeDelete: true)
            .ForeignKey("dbo.ContactGroups", t => t.ContactGroupId, cascadeDelete: true)
            .ForeignKey("dbo.Emails", t => t.EmailId, cascadeDelete: true)
            .Index(t => t.CampaignId)
            .Index(t => t.EmailId)
            .Index(t => t.ContactGroupId);
        
        CreateTable(
            "dbo.EmailTemplates",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    Name = c.String(),
                    Markup = c.String(),
                    Deleted = c.Boolean(nullable: false),
                })
            .PrimaryKey(t => t.Id);
        
    }
    
    public override void Down()
    {
        DropForeignKey("dbo.EmailSends", "EmailId", "dbo.Emails");
        DropForeignKey("dbo.EmailSends", "ContactGroupId", "dbo.ContactGroups");
        DropForeignKey("dbo.EmailSends", "CampaignId", "dbo.Campaigns");
        DropForeignKey("dbo.Emails", "CampaignId", "dbo.Campaigns");
        DropForeignKey("dbo.Contacts", "ContactGroupId", "dbo.ContactGroups");
        DropIndex("dbo.EmailSends", new[] { "ContactGroupId" });
        DropIndex("dbo.EmailSends", new[] { "EmailId" });
        DropIndex("dbo.EmailSends", new[] { "CampaignId" });
        DropIndex("dbo.Emails", new[] { "CampaignId" });
        DropIndex("dbo.Contacts", new[] { "ContactGroupId" });
        DropTable("dbo.EmailTemplates");
        DropTable("dbo.EmailSends");
        DropTable("dbo.Emails");
        DropTable("dbo.Contacts");
        DropTable("dbo.ContactGroups");
        DropTable("dbo.Campaigns");
    }
}

So what's the best way to fix this problem and apply this migration now?

Community
  • 1
  • 1
Ortund
  • 8,095
  • 18
  • 71
  • 139

1 Answers1

0

Just remove the CreateTable method targeting dbo.Campaigns from the generated migration. For future migrations this will not be added because the model gets actually serialized in the __MigrationHistory table and it knows that you already took action on it.

This gets recorded on the Model column in __MigrationHistory table and it knows that Campaigns already exists and it will not try to create it again.

Hope this helps

Arturo Martinez
  • 3,737
  • 1
  • 22
  • 35