c# - 我如何在 EF Code First 中声明数据类型为 Medium Blob 的表上的字段

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

我正在使用 Dotnet Core EntityFramework 和 SapientGuardian.EntityFrameworkCore.MySql

我有一个数据库实体,它有一个名为 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 数据库中创建时,它会创建一个 BLOB 数据类型。

我的问题是如何将它设置为 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);
    }
}

最佳答案

您可以在 DbContext 中使用 Fluent API:

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

关于c# - 我如何在 EF Code First 中声明数据类型为 Medium Blob 的表上的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46199370/

相关文章:

php - 如何从 codeigniter 模型中获取结果作为对象

c# - 我可以在 linq 中克隆 IQueryable 吗?出于联合目的?

c# - Azure Blob 存储 : DownloadToStreamAsync downloading 0kb streams

c# - 如何将 C# 控制台转换为 Powershell?

c# - C# 中需要可选的 VB 参数

mysql - RMySQL 死锁解决方法

c# - 无法删除文件,复制和移动在 C# VS2013 中不起作用

php - mysqli_autocommit 是否适用于准备好的语句?

asp.net-mvc - Windows Azure 上使用 Entity Framework 进行 Asp.net mvc 身份验证

c# - 比较在 Order by 中不起作用?