如何使用 EF 4.0 和模型优先方法将图像存储在数据库中。微控制器2

发布于 2024-10-10 01:26:02 字数 708 浏览 5 评论 0原文

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

alt text

在我的控制器中,我有:

 //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 时遇到异常。图像。 例外情况类似于:方法不存在。

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

I'm trying out the EF 4.0 and using the Model first approach. I'd like to store images into the database and I'm not sure of the best type for the scalar in the entity.
I currently have it(the image scalar type) setup as a binary. From what I have been reading the best way to store the image in the db is a byte[]. So I'm assuming that binary is the way to go. If there is a better way I'd switch.

alt text

In my controller I have:

 //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);
 }

This builds fine but when I run it I get an exception writing the stream to keyToAdd.Image.
The exception is something like: Method does not exist.

Any ideas?
Note that when using a EF 4.0 model first approach I only have int16, int32, double, string, decimal, binary, byte, DateTime, Double, Single, and SByte as available types.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷︶言冷语的世界 2024-10-17 01:26:02

我不应该使用“写入”,而应该使用“读取”。我仍然感兴趣是否有更好的方法来存储图像流。

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

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

Instead of using Write I should have been using Read. I'm still interested if there is a better way to store the image stream.

It was file.InputStream.Write(tmpBytes, 0, file.ContentLength);

 byte[] tmpBytes = new byte[file.ContentLength];
 file.InputStream.Read(tmpBytes, 0, file.ContentLength);
 keyToAdd.Image = tmpBytes;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文