How to store images using Entity Framework Code First CTP 5?

前端 未结 3 1076
遥遥无期
遥遥无期 2020-12-02 07:42

I\'m just trying to figure out if there is a simple way to store and retrieve binary (file) data using EF Code First CTP 5? I would really like it to use the FILESTREAM type

3条回答
  •  青春惊慌失措
    2020-12-02 08:23

    Just declare your property as byte[] as Ladislav mentioned.

    public class Product
    {
        public int Id { get; private set; }
    
        public string Name { get; set; }
    
        public byte[] ProductImage { get; set; }
    }
    

    That is pretty much it. If you don't map the property the convention is it maps to a varbinary(max). If you have an image column in the database already just add [Column(TypeName = "image")] on the ProductImage property or if you prefer code mapping add this to your OnModelCreating override in the context class:

    modelBuilder.Entity().Property(p => p.ProductImage).HasColumnType("image");
    

    The problem I have with it is that I have not found a way to make the property lazy as I don't necessarily want to load binary data every time I fetch a product. I not sure I recall correctly but NHibernate can do it out of the box.

提交回复
热议问题