Generate ntext/nvarchar column when using Entity Framework Code First and a DBMigration

c# entity-framework entity-framework-6 sql-server-ce

Question

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.

1
4
2/16/2014 8:09:45 AM

Popular Answer

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; }
4
1/25/2016 11:44:14 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow