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");
}
}
}