【发布时间】:2015-05-27 12:53:57
【问题描述】:
我正在开发一个 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 命令时,我得到了这个错误
“找不到对象“dbo.course”,因为它不存在或者您没有权限。”
这就是迁移的样子。
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 的所有问题,但我似乎仍然无法弄清楚为什么会发生这种情况以及我需要做些什么来解决它。
任何帮助将不胜感激。在此先感谢
【问题讨论】:
-
从未使用过实体框架,但在
Up()部分,第一步是重命名表,然后是引用旧表名的其他步骤。第一步不会重命名表,从而使引用旧表名的所有其他行无效吗? -
是的,您的权利似乎正在重命名表格,我不明白为什么,因为这不是我对模型所做的更改。我不确定这是否是错误的原因,但我将尝试更改迁移中的一些代码行,看看它是否有帮助。我对 Entity-framework 还是很陌生,我才刚刚开始掌握它。 @SchmitzIT
-
看起来您将其更改为多对多关系并且 EF 不理解。查看 Fluent API,您可以在其中指定关系。 msdn.microsoft.com/en-us/data/jj591620.aspx
-
我终于让它工作了。 @SteveGreene 你在哪里对。框架不明白我打算做什么。它试图重命名课程表,而不是创建一个新表,该表将用于保存课程表和 paymentOption 表的键,以允许两个表之间的多对多关系。谢谢。
标签: c# sql-server asp.net-mvc-5 entity-framework-6 entity-framework-migrations