我正在尝试使用Entity Framework模型修改SQL Server表,但出现此错误:
参数@objname模棱两可或声明的@objtype(OBJECT)错误
我该如何解决这个问题?
我使用以下命令来做到这一点。
Add-Migration "comments",
Update-Database
PM> Update-Database -Verbose
Using StartUp project 'API'.
Using NuGet project 'Data'.
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Target database is: 'template' (DataSource: localhost, Provider: System.Data.SqlClient, Origin: Configuration).
Applying explicit migrations: [201905201819586_HandoverNote].
Applying explicit migration: 201905201819586_HandoverNote.
EXECUTE sp_rename @objname = N'dbo.HandoverNotes', @newname = N'HandoverNotes', @objtype = N'OBJECT'
IF object_id('[PK_dbo.HandoverNotes]') IS NOT NULL BEGIN
EXECUTE sp_rename @objname = N'[PK_dbo.HandoverNotes]', @newname = N'PK_dbo.HandoverNotes', @objtype = N'OBJECT'
END
System.Data.SqlClient.SqlException (0x80131904): Either the parameter @objname is ambiguous or the claimed @objtype (OBJECT) is wrong.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
代码:
namespace Data.Migrations
{
using System;
using System.Data.Entity.Migrations;
public partial class HandoverNote : DbMigration
{
public override void Up()
{
RenameTable(name: "dbo.HandoverNotes", newName: "HandoverNotes");
DropForeignKey("dbo.HandoverNotes", "BusinessUnitID", "dbo.BusinessUnit");
DropIndex("dbo.HandoverNotes", new[] { "BusinessUnitID" });
DropPrimaryKey("dbo.HandoverNotes");
AddColumn("dbo.HandoverNotes", "RecID", c => c.Int(nullable: false, identity: true));
AddColumn("dbo.HandoverNotes", "message", c => c.String());
AddColumn("dbo.HandoverNotes", "LastUpdatedDate", c => c.DateTime(nullable: false));
AddColumn("dbo.HandoverNotes", "BusinessUnit", c => c.String());
AlterColumn("dbo.HandoverNotes", "CreatedBy", c => c.String());
AddPrimaryKey("dbo.HandoverNotes", "RecID");
DropColumn("dbo.HandoverNotes", "ID");
DropColumn("dbo.HandoverNotes", "BusinessUnitID");
DropColumn("dbo.HandoverNotes", "ParentNoteID");
DropColumn("dbo.HandoverNotes", "AssetID");
DropColumn("dbo.HandoverNotes", "Description");
DropColumn("dbo.HandoverNotes", "Status");
DropColumn("dbo.HandoverNotes", "UpdatedDate");
DropColumn("dbo.HandoverNotes", "UpdatedBy");
}
public override void Down()
{
AddColumn("dbo.HandoverNotes", "UpdatedBy", c => c.String(nullable: false, maxLength: 255));
AddColumn("dbo.HandoverNotes", "UpdatedDate", c => c.DateTime(nullable: false));
AddColumn("dbo.HandoverNotes", "Status", c => c.String());
AddColumn("dbo.HandoverNotes", "Description", c => c.String());
AddColumn("dbo.HandoverNotes", "AssetID", c => c.String());
AddColumn("dbo.HandoverNotes", "ParentNoteID", c => c.Int(nullable: false));
AddColumn("dbo.HandoverNotes", "BusinessUnitID", c => c.Int(nullable: false));
AddColumn("dbo.HandoverNotes", "ID", c => c.Int(nullable: false, identity: true));
DropPrimaryKey("dbo.HandoverNotes");
AlterColumn("dbo.HandoverNotes", "CreatedBy", c => c.String(nullable: false, maxLength: 255));
DropColumn("dbo.HandoverNotes", "BusinessUnit");
DropColumn("dbo.HandoverNotes", "LastUpdatedDate");
DropColumn("dbo.HandoverNotes", "message");
DropColumn("dbo.HandoverNotes", "RecID");
AddPrimaryKey("dbo.HandoverNotes", "ID");
CreateIndex("dbo.HandoverNotes", "BusinessUnitID");
AddForeignKey("dbo.HandoverNote", "BusinessUnitID", "dbo.BusinessUnit", "ID", cascadeDelete: true);
RenameTable(name: "dbo.HandoverNotes", newName: "HandoverNote");
}
}
}