找不到对象“dbo.course”,因为它不存在或者您没有权限

时间:2015-03-24 09:18:23

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

我正在开发一个ASP.NET MVC5 Web应用程序。我正在使用Entity Framework 6来满足我的数据存储需求。我正在使用它的代码第一个功能,启用了迁移。自动迁移设置为false。

我有两张桌子,我试图改变。第一个表叫做课程,这就是它的模型。

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

第二个表的名称是paymentOptions,这是它​​的模型。

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

我已经添加了5次迁移,但它们运行良好。现在我尝试添加第六个,我试图对表进行以下更改

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

当我添加迁移它工作正常但是一旦我点击update-database命令突然间我得到了这个错误

&#34;找不到对象&#34; dbo.course&#34;因为它不存在或您没有权限。&#34;

这就是迁移的样子。

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

我已经阅读了有关stackoverflow,http://forums.asp.net,msdn.microsoft.com> SQL Server的所有问题,我仍然无法弄清楚为什么会发生这种情况以及我需要做些什么来修复它

非常感谢任何帮助。提前致谢

0 个答案:

没有答案