0

I am doing a school project where i need to work with blob images with ef core.

I can query blob and convert to wpf BitmapImage, but when I want to reverse the process and I want to upload a selected image to the table I get into barrier.

So when I try to insert a 6.9kB picture to the table it is working great, but if I try a bit larger ~12kB it is not work.

I have a table called Products with the following columns: [Id],[Brand],[Name],[Image], ... other fields

This is my insert command:

...
OpenFileDialog ofd = new OpenFileDialog();
using(var ctx = new Context())
{
      Product newProduct = new Product()
      {
                ProductId = Guid.NewGuid().ToString(),
                Brand = ...,
                Name = ...,
                Image = File.ReadAllBytes(ofd.FileName),
      };
      ctx.Products.Add(newProduct);
      ctx.SaveChanges();
}

This is Product model class:

// It is longblob in mysql
modelBuilder.Entity<Product>(entity => 
{
    entity.ForMySQLHasCollation("utf8_hungarian_ci");
    entity.Property(x=>x.Image).HasMaxLength(64 * 1024 * 1024); //I want max 64Mb
});

Please give me directions.

  • I changed max_allowed_packet=64Mb in mysql
  • Changed the size of Image column
  • I tried 3 way to make byte[] from image
Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
  • Have you seen https://stackoverflow.com/questions/34358528/c-sharp-saving-images-to-mysql-database-as-blob – Andy Feb 17 '23 at 20:29
  • I tried something similar before, but it didn't work for me. – Gyula Nemeth Feb 17 '23 at 20:33
  • 1
    You write that "it is not work". Please explain why it doesn't work. Does it give you an exception? If so, please edit your question to include the full error message and stacktrace of this exception. If you don't get an exception, please describe why your code isn't working. – Luke Woodward Feb 17 '23 at 21:18
  • Sorry for inaccuracy. If the byte[] size greater than 8kb the entity does not insert it to database. For example Image is >8kb and i call ctx.Products.Add(new Product{Name="test", Brand="test",Image=[BYTE ARRAY]}); It will store the new record without image. Like [Name: Test, Brand: Test, Image: nothing] – Gyula Nemeth Feb 17 '23 at 21:26
  • 1
    Switch to `MySqlConnector` - right MySql protocol implementation. In your case replace provider with [Pomelo.EntityFrameworkCore.MySql](https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql#pomeloentityframeworkcoremysql) – Svyatoslav Danyliv Feb 18 '23 at 07:12
  • Pomelo doing the same thing. HaxMaxLength not working correctly or i don't know, but byte[] size fixed at 8000, and if i want to insert a larger image the size switch to -1... I guess i give up on this. – Gyula Nemeth Feb 18 '23 at 15:04

0 Answers0