C# EF CodeFirst:参数@objname 不明确或声称的@objtype (COLUMN) 是错误的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14848385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EF CodeFirst: Either the parameter @objname is ambiguous or the claimed @objtype (COLUMN) is wrong
提问by saber
I've a table named EducationTypes
and an Entity named EducationType
, I renamed one of entity properties, now I'm frequently getting Either the parameter @objname is ambiguous or the claimed @objtype (COLUMN) is wrong
. How can I solve this issue?
我有一个名为 的表EducationTypes
和一个名为的实体EducationType
,我重命名了一个实体属性,现在我经常得到Either the parameter @objname is ambiguous or the claimed @objtype (COLUMN) is wrong
. 我该如何解决这个问题?
The generated SQL Script:
生成的 SQL 脚本:
EXECUTE sp_rename @objname = N'dbo.EducationTypes.nvarchar', @newname = N'EducationTypeTitle', @objtype = N'COLUMN'
回答by MDWeb
Just spent far too much time trying to figure out why this was happening on a production database I can only access via mylittlesql. Couldn't reproduce the problem but made this script from bits of sp_rename so when it does happen next time I can find out exactly why. Yes is overkill, but might help somebody else.
只是花了太多时间试图弄清楚为什么这会发生在我只能通过 mylittlesql 访问的生产数据库上。无法重现该问题,但根据 sp_rename 的位制作了此脚本,以便下次确实发生时我可以确切地找出原因。是的有点矫枉过正,但可能会帮助别人。
There is an issue if you ever somehow manage to get '[' or ']' into the actual column name as stored in sys.columns, (? 'nvarchar' as your column name ???? ). PARSENAME doesn't cope with []'s and returns null, so sp_rename won't work.
如果您以某种方式设法将 '[' 或 ']' 放入存储在 sys.columns 中的实际列名中,则会出现问题(? 'nvarchar' 作为您的列名 ???? )。PARSENAME 不处理 [] 并返回 null,因此 sp_rename 不起作用。
This will only help diagnose the issue for the 'column' case with the error code 15248 which is where I keep having this issue:
这只会帮助诊断错误代码为 15248 的“列”案例的问题,这是我一直遇到这个问题的地方:
declare @objname nvarchar(1035) = N'dbo.EducationTypes.nvarchar' -- input to sp_rename
declare @newname sysname = N'EducationTypeTitle' -- input to sp_rename
declare @UnqualOldName sysname,
@QualName1 sysname,
@QualName2 sysname,
@QualName3 sysname,
@OwnAndObjName nvarchar(517),
@SchemaAndTypeName nvarchar(517),
@objid int,
@xtype nchar(2),
@colid int,
@retcode int
select @UnqualOldName = parsename(@objname, 1),
@QualName1 = parsename(@objname, 2),
@QualName2 = parsename(@objname, 3),
@QualName3 = parsename(@objname, 4)
print 'Old Object Name = ''' + convert(varchar,isnull(@UnqualOldName ,'')) + ''''
-- checks that parsename is getting the right name out of your @objname parameter
print 'Table name:'
if @QualName2 is not null
begin
print QuoteName(@QualName2) +'.'+ QuoteName(@QualName1)
select @objid = object_id(QuoteName(@QualName2) +'.'+ QuoteName(@QualName1))
end
else
begin
print QuoteName(@QualName1)
select @objid = object_id(QuoteName(@QualName1))
end
-- check if table is found ok
print 'Table Object ID = ''' + convert(varchar,isnull(@objid ,-1)) + ''''
select @xtype = type from sys.objects where object_id = @objid
print '@xtype = ''' + convert(varchar,isnull(@xtype,'')) + ''' (U or V?)'
if (@xtype in ('U','V'))
begin
print 'select @colid = column_id from sys.columns where object_id = ' +
convert(varchar,isnull(@objid,0)) + ' and name = ''' +
@UnqualOldName + ''''
select * from sys.columns where object_id = @objid -- and name = @UnqualOldName
select @colid = column_id from sys.columns
where object_id = @objid and name = @UnqualOldName
print 'Column ID = ''' + convert(varchar,isnull(@colid,-1)) + ''''
end
This will output some helpful messages in the Messages tab (of SSMS or whatever you are using) and the table fields in the Results tab.
这将在消息选项卡(SSMS 或您使用的任何内容)和结果选项卡中的表字段中输出一些有用的消息。
Good luck.
祝你好运。
回答by RizJa
If you're using Code First and have (an) existing Migration script(s) and are trying to overwrite a change (i.e. renaming a column) that has since been deleted, then you'll get that error output. Simplest way is to delete the migration script, Add-Migration via NuGet, and then update the database.
如果您正在使用 Code First 并且拥有(一个)现有的迁移脚本并试图覆盖已被删除的更改(即重命名列),那么您将获得该错误输出。最简单的方法是删除迁移脚本,通过 NuGet 添加迁移,然后更新数据库。
回答by Amirhossein Mehrvarzi
This is because of name Conflict of Class (Model) nameswith other reserved or generated ones, when auto creates the tables and ... .
这是因为当自动创建表和...时,类(模型)名称与其他保留或生成的名称冲突。
Considering that EF Code First creates the intervene tables to relate 2 or more tables using name of tables for derived intervene table, so when you use a class name that employs a name like the intervene tables, we'll get such this ambiguous error.
考虑到 EF Code First 创建干预表以使用派生干预表的表名关联 2 个或更多表,因此当您使用使用干预表之类的名称的类名时,我们会得到这样的模糊错误。
For example if you have a Questionclass which has an Answernavigation property the internal model metadata will contain a reference called QUESTION_ANSWER
例如,如果您有一个具有Answer导航属性的Question类,则内部模型元数据将包含一个名为QUESTION_ANSWER的引用
To solve this, try to change the class names (used for generating tables) and ensure their uniqueness.
要解决这个问题,请尝试更改类名(用于生成表)并确保它们的唯一性。
回答by garryp
I got this with Entity Framework 6 when trying to rename a foreign key in my migrations script using the Sql(" ... ") method. The workaround I had was to use square brackets around the name:
在尝试使用 Sql(" ... ") 方法重命名迁移脚本中的外键时,我在 Entity Framework 6 中得到了这个。我的解决方法是在名称周围使用方括号:
i.e. changing this:
即改变这个:
sp_rename 'FK_dbo.tablename_dbo.othertablename_fieldname', 'FK_dbo.tablename_dbo.othertablenewname_fieldnewname', 'object'
...to this:
...到这个:
sp_rename '[FK_dbo.tablename_dbo.othertablename_fieldname]', 'FK_dbo.tablename_dbo.othertablenewname_fieldnewname', 'object'
SQL Server is then able to find the foreign key.
SQL Server 然后能够找到外键。
回答by radu florescu
Actually this error also happens when you just deleted the database, and your context does not realize that your database is not there.
实际上,当您刚刚删除数据库时也会发生此错误,并且您的上下文没有意识到您的数据库不存在。
I recreated the database, and now the error was resolved.
我重新创建了数据库,现在错误已解决。
P.S. make sure you check database is still there when you try to run the update-database
PS确保您在尝试运行更新数据库时检查数据库仍然存在
回答by Br2
I just had the same issue, also after refactoring. For me, the problem was caused by a migration that was refactored as well.
我刚刚遇到了同样的问题,也在重构之后。对我来说,问题是由重构的迁移引起的。
The result was that another migration could not be executed because that migration was looking for a table by searching it's old name.
结果是无法执行另一个迁移,因为该迁移正在通过搜索旧名称来查找表。
Reverting the changes in the migration solved this issue.
恢复迁移中的更改解决了这个问题。
回答by ShhTot
Steer clear of reserved words or class names in your migration title.
避免在迁移标题中使用保留字或类名。
This happened to me when I named a migration "Init" - renamed to "InitialCreate" and all worked perfectly
当我将迁移命名为“Init”时发生了这种情况 - 重命名为“InitialCreate”并且一切正常
回答by Sapan Ghafuri
To me it happened when:
对我来说,它发生在:
- Added a new migration (migratoin1)
- Updated on the local database
- Then deleted the same migration (migratoin1)
- Then added with the same name (migratoin1) another migration
- Then applied to the local database and published.
- 添加了新的迁移 (migratoin1)
- 在本地数据库上更新
- 然后删除同一个迁移(migratoin1)
- 然后添加同名 (migratoin1) 另一个迁移
- 然后应用到本地数据库并发布。
Deleting the migration file (migratoin1) solved my problem.
删除迁移文件 (migratoin1) 解决了我的问题。
回答by Asma Liaquat
I resolved this error by deleting all the old migrations from My database Migration history table in SQL server and then adding a new one but only for the desired changes and then updated database.It worked fine.
我通过从 SQL Server 中的我的数据库迁移历史记录表中删除所有旧迁移,然后添加一个新迁移,但仅针对所需的更改,然后更新数据库来解决此错误。它工作正常。