Entity Framework create field Image

2

I am working with Entity Framework code first I have the need to create an image field in SQL Server. What I'm trying to do with EF is to create an Image field in the database.

 public byte[] Imagen { get; set; }

Product ClassMap

Property(c => c.Imagen).HasMaxLength(8).HasColumnOrder(8);

What should I put in ProductoMap so that in the db I create a field of type Image?

    
asked by Pedro Ávila 05.07.2016 в 17:58
source

2 answers

2

You can use the .HasColumnType() method to make explicit the type of data you want the Fluent API to map.

However, the type image is already obsolete:

From: ntext, text and image (Transact-SQL)

  

The ntext, text, and image data types will be removed in a future version of Microsoft SQL Server. Avoid using it in new development jobs and think about modifying the applications that currently use them. Use nvarchar (max), varchar (max) and varbinary (max) instead.

Instead use the type varbinary(max)

Property(c => c.Imagen).HasColumnType("varbinary(max)");

Although this is not necessary anyway because the SQL Server data type used by default to map the byte[] is already varbinary(max)

If in any case for compatibility or that you want to use image simply use that type:

Property(c => c.Imagen).HasColumnType("image");
    
answered by 05.07.2016 / 18:12
source
1

You do not have to define anything in ProductoMap

[Entity Framework] Code First - Convention for Properties / Columns

By convention a property of type byte[] is a varbinary

    
answered by 05.07.2016 в 18:16