Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF migration : Error changing an indexed field to nullable

The code in my initial migration was as follows

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

So that the Description field would be unique I added the following to the end of the UP method

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

Later I decided to make the Description field required.

The new migration generates the following change

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

However when this change attempts to run I get an error

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

I was able to isolate the SQL line using the profiler, as

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

Which gives me the following error when I run it in 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.

How can I get the migration code to drop the index, then change alter the column and then re-build the index ?

I am using SQL Server 2008 R2

like image 314
Kirsten Avatar asked Apr 03 '13 11:04

Kirsten


People also ask

Is it safe to delete data in an EF/CF migration?

Changing column to 'nullable' (providing it's not an index) or adding new columns (your dbLock) - should not delete data. And most of the time EF/CF migrations are to be trusted to do just the minimal changes on your existing Db structure - of course depending on what you did change (I'd be careful with renaming etc.).

Does EF Core support nullable reference types?

This page introduces EF Core's support for nullable reference types, and describes best practices for working with them. The main documentation on required and optional properties and their interaction with nullable reference types is the Required and Optional Properties page. It is recommended you start out by reading that page first.

Why can’t I use entity types with nullable reference types?

When nullable reference types are enabled, the C# compiler emits warnings for any uninitialized non-nullable property, as these would contain null. As a result, the following, common way of writing entity types cannot be used: Constructor binding is a useful technique to ensure that your non-nullable properties are initialized:

Will set somefield to NOT NULL in Entity Framework?

Will set somefield to Not Null in database, How can I set somefield to allow NULLs?, I tried setting it through SQL Server Management Studio but Entity Framework set it back to Not Null. Just omit the [Required] attribute from the string somefield property. This will make it create a NULL able column in the db.


1 Answers

Something like this perhaps?

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

I think you can also execute SQL direct as below.

Sql("DROP INDEX [IX_Description] ON [dbo].[Sites] WITH ( ONLINE = OFF )");

Which can be useful if you want to add a check the index exists or something.

like image 70
Mark Avatar answered Dec 06 '22 15:12

Mark