How to map an image attribute by Fluent API?

Asked

Viewed 129 times

7

I would like to know how to map an attribute of the type image of SqlServer by the Fluent API.

In my bank I have the following table:

CREATE TABLE [dbo].[ProdutoFotoERP](        
    [ProdutoFotoID] [int] NOT NULL,
    [ProdutoID] [int] NOT NULL,
    [Foto] [image] NULL,
 CONSTRAINT [PK_ProdutosFotoERP] PRIMARY KEY CLUSTERED 
(
    [ProdutoFotoID] ASC
)) 

And I created my entity as follows:

public class ProdutoFotoERP
{
    public int ProdutoFotoID { get; set; }
    public int ProdutoID { get; set; }
    public byte[] Foto { get; set; }

    public virtual ProdutoERP ProdutoERP { get; set; }
}

At first my configuration class is like this:

public class ProdutoFotoERPConfiguration : EntityTypeConfiguration<ProdutoFotoERP>
{
    public ProdutoFotoERPConfiguration()
    {
        ToTable("ProdutoFotoERP");

        HasKey(c => c.ProdutoFotoID);
        Property(c => c.ProdutoFotoID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    }
}
  • Dude, the guy image is deprecated and will be removed in the next versions of SQL Server. If you are starting the system now, you better use varbinary(max).

  • 1

    Unfortunately it is a legacy system and I am making improvements to it, if I could, even image would not write to the database, only the path from where it is.

  • Managed to solve?

  • @jbueno just tested and worked perfectly, thank you very much

1 answer

4


The only thing you need to do is use the method HasColumnType.

Note that the types text, ntext and image are obsolete and will be removed in future versions of SQL Server.

public class ProdutoFotoERPConfiguration : EntityTypeConfiguration<ProdutoFotoERP>
{
    public ProdutoFotoERPConfiguration()
    {
        ToTable("ProdutoFotoERP");

        HasKey(c => c.ProdutoFotoID);
        Property(c => c.ProdutoFotoID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

        Property(p => p.Foto).HasColumnName("Foto").HasColumnType("image");
    }
}
  • Now a doubt, would my entity be a byte[] type even? If it is, when saving in the database it would understand it normally and save correctly?

  • I think so, but that’s another question, right.

  • That was my main question

  • I just saw, and that’s right there, you want to save an array of bytes. It’s right

  • Vlw, I’ll test it here and tell you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.