gpt4 book ai didi

database - 如何使用模型优先方法使用 EF 4.0 将图像存储在数据库中。 MVC2

转载 作者:搜寻专家 更新时间:2023-10-30 20:45:09 24 4
gpt4 key购买 nike

我正在试用 EF 4.0 并使用模型优先方法。我想将图像存储到数据库中,但不确定实体中标量的最佳类型。
我目前将它(图像标量类型)设置为二进制文件。从我一直在阅读的内容来看,将图像存储在数据库中的最佳方式是字节[]。所以我假设二进制是要走的路。如果有更好的方法,我会切换。

alt text

在我的 Controller 中我有:

 //file from client to store in the db
HttpPostedFileBase file = Request.Files[inputTagName];
if (file.ContentLength > 0)
{
keyToAdd.Image = new byte[file.ContentLength];
file.InputStream.Write(keyToAdd.Image, 0, file.ContentLength);
}

这个构建很好,但是当我运行它时,我得到一个异常,将流写入 keyToAdd.Image。异常类似于:方法不存在。

有什么想法吗?请注意,当使用 EF 4.0 模型优先方法时,我只有 int16、int32、double、string、decimal、binary、byte、DateTime、Double、Single 和 SByte 作为可用类型。谢谢

最佳答案

我应该使用 Read 而不是 Write。如果有更好的方法来存储图像流,我仍然很感兴趣。

它是 file.InputStream.Write(tmpBytes, 0, file.ContentLength);

 byte[] tmpBytes = new byte[file.ContentLength];
file.InputStream.Read(tmpBytes, 0, file.ContentLength);
keyToAdd.Image = tmpBytes;

关于database - 如何使用模型优先方法使用 EF 4.0 将图像存储在数据库中。 MVC2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4571508/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com