0

I'm following this tutorial here http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/creating-a-more-complex-data-model-for-an-asp-net-mvc-application

I executed add-Migration Chap4 in my Package Manager Console because I wanted to add a migration and update the database. I have to create 3 new columns into my database because I created 3 new classes (Administrator,Depot and Department).

This error pops up in the PMC:

Unable to generate an explicit migration because the following explicit migrations are pending: [201602172151213_InitialCreate]. Apply the pending explicit migrations before attempting to generate a new explicit migration.

User.cs

public class User
{

    public int UserID { get; set; }
    [StringLength(50, MinimumLength = 1)]
    public string LastName { get; set; }
    [StringLength(50, MinimumLength = 1, ErrorMessage = "First name cannot be longer than 50 characters.")]

    [Column("FirstName")]
    public string FirstMidName { get; set; }

    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime EnrollmentDate { get; set; }

    public string FullName
    {
        get { return LastName + ", " + FirstMidName; }
    }
    public int AdministratorID { get; set; }
    [ForeignKey("AdminID")]
    public virtual Administrator Administrator { get; set; }

    public int DepartmentID { get; set; }
    [ForeignKey("DepartmentID")]
    public virtual Department Department { get; set; }

    public int DepotID { get; set; }
    [ForeignKey("DepotID")]
    public virtual Depot Depot { get; set; }


    public int TicketID { get; set; }
    [ForeignKey("TicketID")]
    public virtual ICollection<Ticket> Tickets { get; set; }
    public virtual ICollection<Ticket> Users { get; set; }

}

Department.cs

public class Department
{
    public int DepartmentID { get; set; }

    [StringLength(50, MinimumLength = 3)]
    public string Name { get; set; }

    public virtual ICollection<User> Users { get; set; }
}

Depot.cs

public class Depot
{
    public int DepotID { get; set; }

    [StringLength(50, MinimumLength = 3)]
    public string Name { get; set; }

    public virtual ICollection<User> Users { get; set; }

}

Administrator.cs

public class Administrator
{
    [Key, ForeignKey("User")]
    public int UserID { get; set; }
    public int AdminID { get; set; }
    public int TicketID { get; set; }        
    [StringLength(50)]
    public string AdminRole { get; set; }
    public virtual ICollection<Ticket> Tickets { get; set; }
    public virtual User User { get; set; }
}

The InitiaCreate.cs

namespace RecreationalServicesTicketingSystem.Migrations
{
    using System;
    using System.Data.Entity.Migrations;

    public partial class InitialCreate : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.User",
                c => new
                {
                    UserID = c.Int(nullable: false, identity: true),
                    LastName = c.String(),
                    FirstMidName = c.String(),
                    EnrollmentDate = c.DateTime(nullable: false),
                })
                .PrimaryKey(t => t.UserID);

            CreateTable(
                "dbo.Ticket",
                c => new
                {
                    TicketID = c.Int(nullable: false, identity: true),
                    CategoryID = c.Int(nullable: false),
                    UserID = c.Int(nullable: false),
                    Issue = c.String(),
                    Priority = c.Int(),
                })
                .PrimaryKey(t => t.TicketID)
                .ForeignKey("dbo.Category", t => t.CategoryID, cascadeDelete: true)
                .ForeignKey("dbo.User", t => t.UserID, cascadeDelete: true)
                .Index(t => t.CategoryID)
                .Index(t => t.UserID);

            CreateTable(
                "dbo.Category",
                c => new
                {
                    CategoryID = c.Int(nullable: false),
                    Title = c.String(),
                })
                .PrimaryKey(t => t.CategoryID);

        }

        public override void Down()
        {
            DropIndex("dbo.Ticket", new[] { "UserID" });
            DropIndex("dbo.Ticket", new[] { "CategoryID" });
            DropForeignKey("dbo.Ticket", "UserID", "dbo.User");
            DropForeignKey("dbo.Ticket", "CategoryID", "dbo.Category");
            DropTable("dbo.Category");
            DropTable("dbo.Ticket");
            DropTable("dbo.User");
        }
    }
}
TykiMikk
  • 1,058
  • 3
  • 15
  • 31
  • Based on the error, it seems that you have a pending migration. You need to run update-database first before doing another migration. – drunkcoder Mar 01 '16 at 07:59
  • I get this error `Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration. You can use the Add-Migration command to write the pending model changes to a code-based migration.` Then after doing `Set DbMigrationsConfiguration.AutomaticMigrationsEnabled` I get the same error – TykiMikk Mar 01 '16 at 08:01
  • Try to delete the pending migration file [201602172151213_InitialCreate] from your Migrations folder and then re-run "add-migration" to create a new migration – drunkcoder Mar 01 '16 at 08:08
  • I did add-migration InitialCreate and it worked but when I do update-database i get the same error as it did in the previous comment. – TykiMikk Mar 01 '16 at 08:16
  • You might have the same case as this. See [link](http://stackoverflow.com/a/33069336/3331310) . Or follow the steps here [link](http://stackoverflow.com/a/16962851/3331310) – drunkcoder Mar 01 '16 at 08:19
  • Do you know what they mean by ContextKey in the tables? – TykiMikk Mar 01 '16 at 08:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/104979/discussion-between-codingangel08-and-jason-wan). – drunkcoder Mar 01 '16 at 08:29

0 Answers0