如何使用 EF 4.0 和模型优先方法将图像存储在数据库中。微控制器2
我正在尝试 EF 4.0 并使用模型优先方法。我想将图像存储到数据库中,但我不确定实体中标量的最佳类型。
我目前将其(图像标量类型)设置为二进制文件。根据我一直在阅读的内容,将图像存储在数据库中的最佳方式是 byte[]。所以我假设二进制是可行的方法。如果有更好的办法我会换。
在我的控制器中,我有:
//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.
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不应该使用“写入”,而应该使用“读取”。我仍然感兴趣是否有更好的方法来存储图像流。
它是 file.InputStream.Write(tmpBytes, 0, file.ContentLength);
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);