aoiTenshi
aoiTenshi

Reputation: 657

.Net6.0 MySQL adding base64 to BLOB type always empty

I have a MVC project which i am using Entity Framework. In database i used MySQL with HeidiSQL. In table i have a column with a BLOB type. I want to store base64 url. I successfully add data but my BLOB type data is always empty.

My object:

public partial class Label
{
    public int Id { get; set; }
    public string? OrderId { get; set; }
    public string? Title { get; set; }
    public string? Sku { get; set; }
    public byte[]? Image { get; set; }
    public string? MimeType { get; set; }
    public string? Status { get; set; }
}

My controller funnction:

[HttpPost]
public IActionResult addInformation(string title,string orderId,string sku, List<string> shipmentInfoList)
{
    foreach(var shipment in shipmentInfoList)
    {
        string b64 = shipment.Split(",")[1];
        byte[] imageData = Convert.FromBase64String(b64);
        string mimetype = shipment.Split(";")[0].Split(":")[1];
        var label = new Label
        {
            Title = title,
            OrderId = orderId,
            Sku = sku,
            Image = imageData,
            MimeType = mimetype,
            Status = "Labeled"
        };

        _context.Labels.Add(label);
    }

    _context.SaveChanges();

    return Json(true);
}

When i try to get FirstOrDefault value its Image variable's count is 0 and in table when i view it it is empty.

My context, OnModelCreating:

    modelBuilder.Entity<Label>(entity =>
    {
        entity.ToTable("labels");

        entity.Property(e => e.Image).HasColumnType("blob");

        entity.Property(e => e.MimeType).HasMaxLength(50);

        entity.Property(e => e.OrderId).HasMaxLength(50);

        entity.Property(e => e.Sku)
            .HasMaxLength(50)
            .HasColumnName("SKU");

        entity.Property(e => e.Status).HasMaxLength(50);

        entity.Property(e => e.Title).HasMaxLength(250);
    });

Upvotes: 0

Views: 92

Answers (0)

Related Questions