new { -6ren">
gpt4 book ai didi

entity-framework-5 - EF 迁移 : Error changing an indexed field to nullable

转载 作者:行者123 更新时间:2023-12-03 23:41:27 24 4
gpt4 key购买 nike

我最初迁移的代码如下

        CreateTable(
"dbo.Sites",
c => new
{
Id = c.Int(nullable: false, identity: true),
Description = c.String(maxLength: 450)
})
.PrimaryKey(t => t.Id);


为了使描述字段是唯一的,我在 UP 方法的末尾添加了以下内容

CreateIndex("dbo.Sites", "Description", unique: true);



后来我决定将描述字段设为必需。

新迁移产生以下变化

AlterColumn("dbo.Sites", "Description", c => c.String(nullable: false, maxLength: 450));



但是,当此更改尝试运行时,我收到一个错误

ALTER TABLE ALTER COLUMN Description failed because one or more objects access this column.



我能够使用探查器隔离 SQL 行,因为

ALTER TABLE [dbo].[Sites] ALTER COLUMN [Description] nvarchar NOT NULL



当我在 Management Studio 中运行它时,它给了我以下错误

Msg 5074, Level 16, State 1, Line 1 The index 'IX_Description' is dependent on column 'Description'. Msg 4922, Level 16, State 9, Line 1 ALTER TABLE ALTER COLUMN Description failed because one or more objects access this column.



如何获取迁移代码以删除索引,然后更改更改列,然后重新构建索引?

我正在使用 SQL Server 2008 R2

最佳答案

也许是这样的?

DropIndex("dbo.Sites", "IX_Description");
AlterColumn("dbo.Sites", "Description", c => c.String(nullable: false, maxLength: 450));
CreateIndex("dbo.Sites", "Description", unique: true);

我认为您也可以直接执行 SQL,如下所示。
Sql("DROP INDEX [IX_Description] ON [dbo].[Sites] WITH ( ONLINE = OFF )");

如果您想添加检查索引是否存在或其他内容,这会很有用。

关于entity-framework-5 - EF 迁移 : Error changing an indexed field to nullable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15785707/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com