.net 如何使用 Entity Framework Code First CTP 5 存储图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4653095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 15:06:57  来源:igfitidea点击:

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

.netentity-frameworksql-server-2008ef-code-first

提问by Max Schmeling

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, but I'm really just looking for some way to make it work.

我只是想弄清楚是否有一种简单的方法可以使用 EF Code First CTP 5 存储和检索二进制(文件)数据?我真的很喜欢它使用 FILESTREAM 类型,但我真的只是在寻找某种方法来使它工作。

采纳答案by Ladislav Mrnka

You can't use SQL FILESTREAMin EF. EF is supposed to work on top of different database servers but filestream feature is specific feature of SQL 2008 and newer. You can try to do it old way - use varbinary(max)in your database table and use byte array in your mapped class.

您不能FILESTREAM在 EF 中使用 SQL 。EF 应该在不同的数据库服务器上工作,但文件流功能是 SQL 2008 和更新版本的特定功能。您可以尝试使用旧方法 -varbinary(max)在数据库表中使用并在映射类中使用字节数组。

Edit:

编辑:

Little clarification - you can use FILESTREAMin the database but EF will not take advantage of streaming. It will load it as standard varbinary(max).

一点澄清 - 您可以FILESTREAM在数据库中使用,但 EF 不会利用流式传输。它将作为标准加载varbinary(max)

回答by nima

I always create another class like ProductImagewith a one-to-one association in order to manage lazy loading and also to normalize the table:

我总是创建另一个类,例如ProductImage一对一关联,以管理延迟加载并规范化表:

public class ProductImage
{
    public int ProductId { get; private set; }
    public byte[] Image { get; set; }
}

回答by Cosmin Onea

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

正如拉迪斯拉夫提到的那样,只需将您的财产声明为 byte[] 即可。

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:

差不多就是这样。如果您不映射该属性,则约定是它映射到varbinary(max). 如果您已经在数据库中有一个图像列,只需添加[Column(TypeName = "image")]ProductImage 属性,或者如果您更喜欢代码映射,请将其添加到上下文类中的 OnModelCreating 覆盖中:

modelBuilder.Entity<Product>().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.

我遇到的问题是我还没有找到一种方法来使属性变得懒惰,因为我不一定要在每次获取产品时都加载二进制数据。我不确定我没记错,但 NHibernate 可以开箱即用。