Hello,
I need to get the length of a field to limit the size of the user input to the spezific length of the field.
example
context = new Sivasdatacontext();
var query = from it in context.Parts
select it;
Parts comes with Name i need to know which length this column has in the DB
How can I do this?
thx
Paul
get length of a field
In Entity Framework, you can use the following code to obtain the MaxLength value:
Code: Select all
var q = from meta in context.MetadataWorkspace.GetItems(DataSpace.CSpace).Where(
m => m.BuiltInTypeKind == BuiltInTypeKind.EntityType)
from p in (meta as EntityType).Properties.Where(
p => p.DeclaringType.Name == entity.GetType().Name
&& p.Name == propertyName && p.TypeUsage.EdmType.Name == "String")
select p;
var queryResult = from meta in db.MetadataWorkspace.GetItems(DataSpace.CSpace).Where(
m => m.BuiltInTypeKind == BuiltInTypeKind.EntityType)
from p in (meta as EntityType).Properties.Where(
p => p.DeclaringType.Name == entity.GetType().Name
&& p.Name == propertyName && p.TypeUsage.EdmType.Name == "String")
select p.TypeUsage.Facets["MaxLength"].Value;
if (queryResult.Count() > 0) {
int maxLength = Convert.ToInt32(queryResult.First());
}