4

I'd like to either using the Fluent API or data annotations be able to automatically generate a database migration class for a string column as an ntext rather than a fixed length string. How would I do that?

When using Entity Framework v6, with Code First, and SQL Server Compact 4, given a simple C# class such as:

public class GameFile
{
    public string Id { get; set; }        
    public string FileData { get; set; }
    public int FileSize { get; set; }
}

Then, when adding a new migration:

add-migration first

the resulting migration class defaults to a string column of maximum length 4000.

CreateTable(
    "dbo.GameFiles",
    c => new
        {
            Id = c.String(nullable: false, maxLength: 4000),
            FileData = c.String(maxLength: 4000),
            FileSize = c.Int()
        })
    .PrimaryKey(t => t.Id);

When the database is updated via update-database, the table will have a column named FileData of type nvarchar(4000).

I've tried adding an attribute of MaxLength and/or Column to the FileData property, but there's no change in the generated migration code.

[MaxLength, Column(TypeName = "ntext")]
public string FileData { get; set; }

I know if I manually edit the migration code to explicitly declare the storeType:

FileData = c.String(storeType: "ntext")

that the resulting table is created properly.

ErikEJ
  • 40,951
  • 5
  • 75
  • 115
WiredPrairie
  • 58,954
  • 17
  • 116
  • 143

1 Answers1

4

Seems like the bug is fixed for Entity Framefork v6.1.3.

The following definition works as expected - creates ntext column.

[Column(TypeName = "ntext")]
public string FileData { get; set; }
MrHant
  • 880
  • 6
  • 11
  • Just a heads up that: "ntext, text, and image data types will be removed in a future version of SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead." Source: https://learn.microsoft.com/en-us/sql/t-sql/data-types/ntext-text-and-image-transact-sql?view=sql-server-2017 – Tohid Nov 23 '18 at 03:22