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

I would like to use either the Fluent API or data annotations to automatically generate a database migration class for a row column as ntext, rather than a fixed-length row. How can I do it?

When using Entity Framework v6 with First code and SQL Server Compact 4, given a simple C # class, for example:

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 default migration final class corresponds to the stringmaximum length column 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 through update-database, the table will have a column with the FileDatatype name nvarchar(4000).

MaxLength / Column FileData, .

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

, , storeType:

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

.

+4
1

, ​​ Entity Framefork v6.1.3.

- ntext.

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

Source: https://habr.com/ru/post/1527130/


All Articles