[英]How to manage to store null byte array in SQL Server with Entity Framework?
I would want to save images in the database with VARBINARY(max)
type by this procedure:我想通过以下过程将图像保存在VARBINARY(max)
类型的数据库中:
ALTER proc [dbo].[User_Create]
(@BirthCertificateImage varbinary(max),
@BookletImage varbinary(max),
@GreenCardImage varbinary(max))
as
insert into [User]
(BirthCertificateImage,BookletImage,GreenCardImage)
values
(@BirthCertificateImage,@BookletImage,@GreenCardImage)
And here is how I managed to extract fields with SQL procedure这是我如何设法使用 SQL 过程提取字段
ALTER proc [dbo].[User_GetById]
(@UserId int)
as
select * from [User]
where
Id = @UserId
And here is how I'm using the mentioned procedure in the EF这是我在 EF 中使用上述程序的方式
var user = await db.Database.SqlQuery<UserModel>("User_GetById @UserId"
, new SqlParameter("UserId",id)).SingleOrDefaultAsync();
And here is how I managed to show the binary image in a PictureBox这是我设法在 PictureBox 中显示二进制图像的方法
var birthCertificateMs = new
MemoryStream(response.Entity.BirthCertificateImage);
birthCertificatePictureBox.Image = Image.FromStream(birthCertificateMs);
birthCertificateMs.Close();
Also, my property's type for Maintenance the image is byte[]
此外,我的资产类型为维护图像是byte[]
So how can I manage it?那么我该如何管理呢? If that was null do I won't get in trouble?如果那是空的,我不会遇到麻烦吗?
Consider, it works correctly when input is not null考虑一下,当输入不为空时它可以正常工作
I would recommend explicitly checking for null/empty before attempting to use the data.我建议在尝试使用数据之前明确检查 null/empty。
if(response.Entity.BirthCertificateImage != null && response.Entity.BirthCertificateImage.Length > 0)
{
using(var birthCertificateMs = new MemoryStream(response.Entity.BirthCertificateImage))
{
birthCertificatePictureBox.Image = Image.FromStream(birthCertificateMs);
}
}
and I would opt to wrap all of that in a Try/Catch block to handle where the byte data doesn't contain an image.我会选择将所有这些都包装在 Try/Catch 块中,以处理字节数据不包含图像的情况。 (It happens, especially as systems mature, someone supports chucking a PDF or such in there.) (它发生了,尤其是随着系统的成熟,有人支持将 PDF 之类的文件放入其中。)
One other bit of advice: for large binary data such as images, I would recommend updating the schema to move these off into a separate table linked in a 1-0..1 relationship with User.还有一点建议:对于像图像这样的大型二进制数据,我建议更新架构以将它们移到与用户以 1-0..1 关系链接的单独表中。 (So a new table called UserImage with a PK of UserId, then User can be configured with a HasOptional on UserImage) The reason for this is that this data is likely not going to be accessed that frequently, where you might Join/Select on "User" fairly often. (所以一个名为 UserImage 的新表,带有 UserId 的 PK,然后可以在 UserImage 上使用 HasOptional 配置 User)原因是这些数据可能不会被频繁访问,您可能会在“上加入/选择”用户”相当频繁。 If you use User as a reference and code eager/lazy-loads a User with those larger blobs, it will have a performance impact.如果您使用 User 作为参考,并使用这些较大的 blob 代码预先/延迟加载 User,则会对性能产生影响。 If the user image data is in a separate related table, you can reference User without a performance cost of that expensive data until it is explicitly needed.如果用户图像数据位于单独的相关表中,则您可以引用 User,而无需考虑该昂贵数据的性能成本,直到明确需要它为止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.