Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i declare a field on a table with the datatype of Medium Blob in EF Code First

Im using dotnet Core EntityFramework using SapientGuardian.EntityFrameworkCore.MySql

I have a database Entity with a property called ProfileImage stored as a byte[] ... extract below

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

When this get created in the MySql Database it creates a BLOB DataType.

My Question is how can set it to be a MediumBlob?

Edit: The Migration (which I forgot to run) produced the following:

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);
    }
}
like image 832
I Bowyer Avatar asked Oct 30 '22 02:10

I Bowyer


1 Answers

You can use the Fluent API in your DbContext:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<ProfileEntity>().Property(p => p.ProfileImage)
        .HasColumnType("MediumBlob");
}
like image 187
Raphael Müllner Avatar answered Nov 14 '22 02:11

Raphael Müllner