实体框架和VARBINARY [英] Entity framework and VARBINARY
问题描述
我使用的是.NET实体框架和我有包含VARBINARY一个实体。有一种简单的方法来获得在codebehind的VARBINARY的大小,这至关重大的被从数据库中检索?
I’m using the .NET entity framework and I’ve got one entity containing a varbinary. Is there an easy way to get the size of the varbinary in the codebehind, efter it’s been retrieved from the database?
我想可能有一些方式直接从实体获得大小,像entity.Context.Size - 或做一件需要以不同方式处理它
I’m thinking there might be some way to get the size directly from the entity, something like entity.Context.Size – or do one need to handle it differently?
推荐答案
一个VARBINARY转化为在实体框架一个byte []字段,这意味着你可以检查数组的长度属性:
A varbinary translates to a byte[] field in the entity framework, which means you can check the Length property of the array:
int fieldSize = entity.MyVarBinaryField.Length;
正如 tster 提到:在LINQ到实体查询,你可以调用<一个href=\"http://msdn.microsoft.com/en-us/library/system.data.objects.sqlclient.sqlfunctions.datalength.aspx\">DataLength SqlFunctions 类的方法,这将转化成的DATALENGTH 函数调用生成的SQL语句。这种方法只适用与SQL Server和Entity Framework 4或更高版本:
As mentioned by tster: In a LINQ to Entities query, you can call the DataLength method of the SqlFunctions class, which will translate into a DATALENGTH function call in the generated SQL statement. This only works with SQL Server and Entity Framework 4 or later:
int? fieldSize = repository.Entity
.Select(e => SqlFunctions.DataLength(e.MyVarBinaryField)).Single();
这篇关于实体框架和VARBINARY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!