Cannot find the object "dbo.course" because it does not exist or you do not have permissions

asp.net-mvc-5 c# ef-migrations entity-framework-6 sql-server

Question

I am developing an ASP.NET MVC5 Web Application. I am using Entity Framework 6 for my data storage needs. I am using the code first feature of it with migrations enabled. automatic migrating set to false.

I have two tables that I'm trying to change. The first Table is called course and this is it's model.

public class course
{
    [Key]
    public int courseID { get; set; }
    public int categoryID { get; set; }
    public int PaymentOptionsID { get; set; }
    [Required]
    [DisplayName("Course Code")]
    public string courseCode { get; set; }
    [Required]
    [DisplayName("Course Name")]
    public string courseName { get; set; }
    [Required]
    [DataType(DataType.Currency)]
    public decimal price { get; set; }
    [DataType(DataType.MultilineText)]
    [DisplayName("Course Description")]
    [StringLength(255, ErrorMessage = "Only a maximum of 255 characters are                                     allowed")]
    public string courseDescription { get; set; }
    [Required]
    [DisplayName("Study Material")]
    public string courseIncludes { get; set; }
    public string courseInfoUrl { get; set; }

    public virtual ICollection<enrollment> enrollment { get; set; }
    public virtual courseCategory courseCategory { get; set; }
    public virtual PaymentOptions PaymentOptions { get; set; }
}

The second table's name is paymentOptions and here is it's model.

public class PaymentOptions
{
    [Key]
    public int PaymentOptionsID { get; set; }
    public int courseID { get; set; }
    [DisplayName("Payment Options")]
    public string paymentOption {get; set; }
}

I have 5 migrations that I've added and they worked fine. Now I'm trying to add the sixth one where I am trying to make the following changes to the tables

public class course
{
    [Key]
    public int courseID { get; set; }
    public int categoryID { get; set; }
    public int PaymentOptionsID { get; set; }
    [Required]
    [DisplayName("Course Code")]
    public string courseCode { get; set; }
    [Required]
    [DisplayName("Course Name")]
    public string courseName { get; set; }
    [Required]
    [DataType(DataType.Currency)]
    public decimal price { get; set; }
    [Required]
    [DisplayName("Course Includes")]
    public string courseIncludes { get; set; }
    public string courseInfoUrl { get; set; }

    public virtual ICollection<enrollment> enrollment { get; set; }
    public virtual courseCategory courseCategory { get; set; }
    public virtual ICollection<PaymentOptions> PaymentOptions { get; set; }
}    

public class PaymentOptions
{
    [Key]
    public int PaymentOptionsID { get; set; }
    public int courseID { get; set; }
    [DisplayName("Payment Options")]
    public string paymentOption {get; set; }

    public virtual ICollection<course> course { get; set; }
}

When I add the Migration It works fine but as soon as I hit the update-database command all of the sudden I get this error

"Cannot find the object "dbo.course" because it does not exist or you do not have permissions."

This is what the migration looks like.

public partial class AddPaymentOptionsagain : DbMigration
{
    public override void Up()
    {
        RenameTable(name: "dbo.course", newName: "PaymentOptionscourse");
        DropForeignKey("dbo.course", "PaymentOptions_PaymentOptionsID", "dbo.PaymentOptions");
        DropIndex("dbo.course", new[] { "PaymentOptions_PaymentOptionsID" });
        AddColumn("dbo.course", "PaymentOptionsID", c => c.Int(nullable: false));
        CreateIndex("dbo.PaymentOptionscourse", "PaymentOptions_PaymentOptionsID");
        CreateIndex("dbo.PaymentOptionscourse", "course_courseID");
        AddForeignKey("dbo.PaymentOptionscourse", "PaymentOptions_PaymentOptionsID", "dbo.PaymentOptions", "PaymentOptionsID", cascadeDelete: true);
        AddForeignKey("dbo.PaymentOptionscourse", "course_courseID", "dbo.course", "courseID", cascadeDelete: true);
        DropColumn("dbo.course", "courseDescription");
        DropColumn("dbo.course", "PaymentOptions_PaymentOptionsID");
    }

    public override void Down()
    {
        AddColumn("dbo.course", "PaymentOptions_PaymentOptionsID", c => c.Int());
        AddColumn("dbo.course", "courseDescription", c => c.String(maxLength: 255));
        DropForeignKey("dbo.PaymentOptionscourse", "course_courseID", "dbo.course");
        DropForeignKey("dbo.PaymentOptionscourse", "PaymentOptions_PaymentOptionsID", "dbo.PaymentOptions");
        DropIndex("dbo.PaymentOptionscourse", new[] { "course_courseID" });
        DropIndex("dbo.PaymentOptionscourse", new[] { "PaymentOptions_PaymentOptionsID" });
        DropColumn("dbo.course", "PaymentOptionsID");
        CreateIndex("dbo.course", "PaymentOptions_PaymentOptionsID");
        AddForeignKey("dbo.course", "PaymentOptions_PaymentOptionsID", "dbo.PaymentOptions", "PaymentOptionsID");
        RenameTable(name: "dbo.PaymentOptionscourse", newName: "course");
    }
}

I have read all the questions on stackoverflow, http://forums.asp.net, msdn.microsoft.com › SQL Server and I still can't seem to figure out why this is happening and what I need to do to fix it.

Any help would be much appreciated. Thanks in advance

1
2
3/24/2015 9:18:22 AM

Popular Answer

I see the problem in your statements sequence. You are trying to refer to the object after you renamed it.

 RenameTable(name: "dbo.course", newName: "PaymentOptionscourse");
    DropForeignKey("dbo.course", "PaymentOptions_PaymentOptionsID", "dbo.PaymentOptions");
    DropIndex("dbo.course", new[] { "PaymentOptions_PaymentOptionsID" });

Instead try this

 RenameTable(name: "dbo.course", newName: "PaymentOptionscourse");
    DropForeignKey("dbo.PaymentOptionscourse", "PaymentOptions_PaymentOptionsID", "dbo.PaymentOptions");
    DropIndex("dbo.PaymentOptionscourse", new[] { "PaymentOptions_PaymentOptionsID" });
    AddColumn("dbo.PaymentOptionscourse", "PaymentOptionsID", c => c.Int(nullable: false));
    CreateIndex("dbo.PaymentOptionscourse", "PaymentOptions_PaymentOptionsID");
    CreateIndex("dbo.PaymentOptionscourse", "course_courseID");
    AddForeignKey("dbo.PaymentOptionscourse", "PaymentOptions_PaymentOptionsID", "dbo.PaymentOptions", "PaymentOptionsID", cascadeDelete: true);
    AddForeignKey("dbo.PaymentOptionscourse", "course_courseID", "dbo.PaymentOptionscourse", "courseID", cascadeDelete: true);
    DropColumn("dbo.PaymentOptionscourse", "courseDescription");
    DropColumn("dbo.PaymentOptionscourse", "PaymentOptions_PaymentOptionsID");
7
11/23/2015 4:35:30 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow