【问题标题】:ASP.net MVC 5 EF6 Error While updating the database更新数据库时 ASP.net MVC 5 EF6 错误
【发布时间】:2015-12-23 11:47:44
【问题描述】:

我对 Asp.net MVC 5 EF6 还是很陌生。我正在为 Microsoft 在 asp.net 网站上提供的 Contoso 大学开发应用程序。在第 11 章 Implementing the Inheritance 在添加继承并通过迁移命令添加迁移之后,它起作用了,但是当我尝试将 update-database 命令应用于 PMC 时,我遇到了这个错误:

错误号:15248,状态:1,类:11 参数@objname 是 模棱两可或声称的@objtype (OBJECT) 是错误的。

这是我的 /inheritance 迁移类的代码。 请指导我进行修复。

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

    public partial class Inheritance : DbMigration
    {
        public override void Up()
        {
            // Drop foreign keys and indexes that point to tables we're going to drop.
            DropForeignKey("dbo.Enrollment", "StudentID", "dbo.Student");
            DropIndex("dbo.Enrollment", new[] { "StudentID" });

            RenameTable(name: "dbo.Instructor", newName: "Person");
            AddColumn("dbo.Person", "EnrollmentDate", c => c.DateTime());
            AddColumn("dbo.Person", "Discriminator", c => c.String(nullable: false, maxLength: 128, defaultValue: "Instructor"));
            AlterColumn("dbo.Person", "HireDate", c => c.DateTime());
            AddColumn("dbo.Person", "OldId", c => c.Int(nullable: true));

            // Copy existing Student data into new Person table.
            Sql("INSERT INTO dbo.Person (LastName, FirstName, HireDate, EnrollmentDate, Discriminator, OldId) SELECT LastName, FirstName, null AS HireDate, EnrollmentDate, 'Student' AS Discriminator, ID AS OldId FROM dbo.Student");

            // Fix up existing relationships to match new PK's.
            Sql("UPDATE dbo.Enrollment SET StudentId = (SELECT ID FROM dbo.Person WHERE OldId = Enrollment.StudentId AND Discriminator = 'Student')");

            // Remove temporary key
            DropColumn("dbo.Person", "OldId");

            DropTable("dbo.Student");

            // Re-create foreign keys and indexes pointing to new table.
            AddForeignKey("dbo.Enrollment", "StudentID", "dbo.Person", "ID", cascadeDelete: true);
            CreateIndex("dbo.Enrollment", "StudentID");
        }

        public override void Down()
        {
            CreateTable(
                "dbo.Student",
                c => new
                    {
                        ID = c.Int(nullable: false, identity: true),
                        LastName = c.String(nullable: false, maxLength: 20),
                        FirstName = c.String(nullable: false, maxLength: 20),
                        EnrollmentDate = c.DateTime(nullable: false),
                    })
                .PrimaryKey(t => t.ID);

            AlterColumn("dbo.Person", "HireDate", c => c.DateTime(nullable: false));
            DropColumn("dbo.Person", "Discriminator");
            DropColumn("dbo.Person", "EnrollmentDate");
            RenameTable(name: "dbo.Person", newName: "Instructor");
        }
    }
}

【问题讨论】:

    标签: asp.net-mvc entity-framework


    【解决方案1】:

    考虑: 1. 删除 Migrations 文件夹中的所有 Migration 文件 2.在连接字符串中重命名你的数据库 3. 运行 PM> add-migration 4. 运行 PM> update-database

    【讨论】:

      猜你喜欢
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      • 2014-11-22
      • 2014-04-10
      • 2013-12-16
      • 2013-02-27
      相关资源
      最近更新 更多