get length of a field

Discussion of open issues, suggestions and bugs regarding Entity Framework support in ADO.NET Data providers
Post Reply
paul.-b
Posts: 5
Joined: Mon 26 Oct 2009 13:51

get length of a field

Post by paul.-b » Mon 26 Oct 2009 14:05

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

AndreyR
Devart Team
Posts: 2919
Joined: Mon 07 Jul 2008 13:16

Post by AndreyR » Tue 27 Oct 2009 14:38

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

Post Reply