简体   繁体   中英

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

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.

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; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM