如何在EF代码优先

时间:2017-09-13 13:49:15

标签: c# mysql entity-framework entity-framework-core

我使用SnetientGuardian.EntityFrameworkCore.MySql

使用dotnet Core EntityFramework

我有一个数据库实体,其名为ProfileImage的属性存储为byte[] ...摘录

public class ProfileEntity
{

    /// Gets or sets the full name.
    /// </summary>
    public string FullName { get; set; }

    /// <summary>
    /// A Byte Array with the profile image Bitmap
    /// </summary>
    public byte[] ProfileImage { get; set; }
}

当在MySql数据库中创建此get时,它会创建一个BLOB DataType。

我的问题是如何将其设置为MediumBlob?

修改 迁移(我忘了运行)产生了以下内容:

public partial class AddMediumBlob : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.AlterColumn<byte[]>(
            name: "ProfileImage",
            table: "ProfileEntity",
            type: "MediumBlob",
            nullable: true,
            oldClrType: typeof(byte[]),
            oldNullable: true);
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.AlterColumn<byte[]>(
            name: "ProfileImage",
            table: "ProfileEntity",
            nullable: true,
            oldClrType: typeof(byte[]),
            oldType: "MediumBlob",
            oldNullable: true);
    }
}

1 个答案:

答案 0 :(得分:2)

您可以在DbContext中使用Fluent API:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<ProfileEntity>().Property(p => p.ProfileImage)
        .HasColumnType("MediumBlob");
}