How to get the maximum length of a string from an EDMX model in code?
Asked Answered
H

3

9

I've created an EDMX object from a database I'm programming against.

I need to get input from a user and save it to a row in the database table. The problem is that I need to limit the length of input strings to the width of the corresponding VARCHAR column in the database.

When I browse the model, I can clearly see in the properties window that the model knows the max length of the string, but I don't know how to access this data in code.

If I want to write something like this:

Entities entities = new Entities();
myTable = entities.myTable.First();
if (userInput.length > myTable.columnA.MaxLength)
{
    // tell the user that the input is too long.
}
else
{
    myTable.columnA = userInput;
}

How do I write it?

Update: I would like to point out that the IObjectContextAdapater mentioned in the answers below is in the System.Data.Entity.Infrastructure namespace.

Hooch answered 19/11, 2014 at 18:57 Comment(2)
I'm surprised by the low number of views on this question. Thanks very much for the helpful answers; I might have never figured it out on my own.Hooch
If you run into a situation where you need to bring MaxLength value of a table column directly from the Database, here's a good answer: https://mcmap.net/q/589681/-get-column-sizes-of-a-table-in-sql-server-using-cKirchhoff
C
17

Here are two methods by which you can read the meta data:

int? GetMaxLength(DbContext context, string tableName, string propertyName)
{
    var oc = ((IObjectContextAdapter)context).ObjectContext;

    return oc.MetadataWorkspace.GetItems(DataSpace.CSpace).OfType<EntityType>()
             .Where(et => et.Name == tableName)
             .SelectMany(et => et.Properties.Where(p => p.Name == propertyName))
             .Select (p => p.MaxLength)
             .FirstOrDefault();
}

int? GetMaxLength<T>(DbContext context, Expression<Func<T, object>> property)
{
    var memberExpression = (MemberExpression)property.Body;
    string propertyName = memberExpression.Member.Name;
    return GetMaxLength(context, typeof(T).Name, propertyName);
}

So you can either enter the table name and property name, or an expression that specifies the property you're interested in.

Another approach could be to create a MetaData class and use the MaxLength attribute.

Conspiracy answered 19/11, 2014 at 19:29 Comment(9)
I'm confused. I thought that the EDMX entities is an ObjectContext, which has nothing to do with DbContext.Hooch
Not necessarily. You'd have to check. If it is an ObjectContext you don't need this ((IObjectContextAdapter)context).ObjectContext part.Conspiracy
I did check and I see that it's a DbContext. To be honest, I have heard of DbContext, but I haven't read up on all the new features of the new Entity Framework versions, so this one got past me. Wow, there is so much keeping up to do in the .net world!Hooch
The function GetItems returns an enumeration of 393 items. However, the OfType<myTable> function, where myTable is a model of a SQL table, returns an empty enumeration.Hooch
You should leave EntityType there, it's an EF class.Conspiracy
Oops, I thought it was a placeholder for my entity type.Hooch
You might want to add "using System.Data.Entity.Core.Metadata.Edm;" and "using System.Data.Entity.Infrastructure;"Hilburn
First example:I am using EF 6.4.4 Nuget package in a .Net Framework 4.8 project. My EF context does not cast to an IObjectContextAdapter object: Unable to cast... to ... error. Second example: I cannot use as there is no indication of what is passed as "property."Buxtehude
@Buxtehude Feel free to post a question in which you have ample opportunity to explain your problem. A comment is not a the best way to ask new questions.Conspiracy
E
3

If you modify the T4 template you can add your own attribute to the properties that have MaxLength set.

If you can find the right place to add it, it's something as simple as this:

var lengthAttributeText = edmProperty.MaxLength.HasValue
   ? string.Format("[MaxLength({0})] ", edmProperty.MaxLength.Value)
   : "";

And then add this into the text for the property line. (Tricky to be more detailed since I've already modified my .tt file a lot; also the lack of proper IDE support for .tt files makes this a lot harder than it could be.)

Enliven answered 29/10, 2019 at 22:44 Comment(0)
O
2

It's not very pretty; reading edmx properties at runtime is not something Microsoft exposed easily or documented well (or in some cases, at all). context is your DBContext.

var objectContext = ((IObjectContextAdapter)context).ObjectContext;
var entityType = objectContext.MetadataWorkspace.GetItems<EntityType>(DataSpace.CSpace).Where(e => e.Name == "your entity name").First();
var facets = entityType.Properties["your property name"].TypeUsage.Facets;

facets will look something like this, so you'll need to look for the MaxLength Name(may not exist, depending on the underlying field type) and get the Value:

Count = 5
    [0]: Nullable=false
    [1]: DefaultValue=null
    [2]: MaxLength=250
    [3]: Unicode=false
    [4]: FixedLength=false
Overflow answered 19/11, 2014 at 19:18 Comment(5)
Is context the instantiation of the entities from the EDMX, or are you talking about something else?Hooch
I'm getting an error with your code. When I write code like your example, I use a class imported from a SQL table for EntityType and I get an error: The type cannot be used as type parameter 'T'... there is no implicit reference conversion from myTable to GlobalItem. What does this mean?Hooch
Leave GetItems<EntityType> be, the only thing you need to replace are the two strings.Overflow
I am using EF 6.4.4 Nuget package in a .Net Framework 4.8 project. My EF context does not cast to an IObjectContextAdapter object: Unable to cast... to ... error.Buxtehude
@PaulAbbott Which version of EF is this code valid against? Is it EF Core or .NET Framework?Gainer

© 2022 - 2024 — McMap. All rights reserved.