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

c# entity-framework entity-framework-core mysql

Question

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);
    }
}
1
4
9/14/2017 8:46:00 AM

Accepted Answer

You can use the Fluent API in your DbContext:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<ProfileEntity>().Property(p => p.ProfileImage)
        .HasColumnType("MediumBlob");
}
2
9/13/2017 1:53:34 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow