none
How to get the MaxLength from a Fluent API mapping specification in runtime? RRS feed

  • Question

  • I need to retrieve the maxlength value specified for a field using Fluent API and Entity Framework 5 Code First.

    I have seen a couple of examples using MetadataWorkspace but it doesn't work when you use Fluent API.

    I just need guidance for getting the maxlenght value but to give some perspective the goal is:

    1. Create new Foo object
    2. Get the maxlength value defined for Bar property using Fluent API.
    3. Apply certain rules based on the maxlength to the string to be assigned to Bar.
    4. Save.

    Thanks in advance.

    Wednesday, December 18, 2013 4:35 PM

Answers

  • Hello,

    >>I just need guidance for getting the maxlenght value but to give some perspective the goal is:

    According to your description of the goal, do you want to create the entity class at runtime?

    If it is, unfortunately, it is impossible.

    If not, in code first, for string type, the default max length is max. Or we can use the Fluent API to specify the max length like below:

    modelBuilder.Entity<Blog>().Property(b => b.Name).HasMaxLength(100);

    Then if we call method using MetadataWorkspace to get the result, it will show 100. My test method is like below:

    public int GetMaxLength<TEntity>(ObjectContext oc, Expression<Func<TEntity, string>> property)
    
                where TEntity : class
    
            {
    
                int MaxLength = 0;
    
                var test = oc.MetadataWorkspace.GetItems(DataSpace.CSpace);
    
    
                if (test == null)
    
                    return -1;
    
    
                Type entType = typeof(TEntity);
    
                string propertyName = ((MemberExpression)property.Body).Member.Name;
    
    
                var q = test
    
                    .Where(m => m.BuiltInTypeKind == BuiltInTypeKind.EntityType)
    
                    .SelectMany(meta => ((EntityType)meta).Properties
    
                    .Where(p => p.Name == propertyName && p.TypeUsage.EdmType.Name == "String"));
    
    
                var queryResult = q.Where(p =>
    
                {
    
                    var match = p.DeclaringType.Name == entType.Name;
    
                    if (!match)
    
                        match = entType.Name == p.DeclaringType.Name;
    
    
                    return match;
    
    
                })
    
                    .Select(sel => sel.TypeUsage.Facets["MaxLength"].Value)
    
                    .ToList();
    
    
                if (queryResult.Any())
    
                {
    
                    string result = queryResult.FirstOrDefault().ToString();
    
                    if (result == "Max")
    
                    {
    
                        MaxLength = -1;
    
                    }
    
                    else
    
                    {
    
                        MaxLength = Convert.ToInt32(result);
    
                    }
    
                }
    
                return MaxLength;
    
            }
    

    However, since we have already specified the max length in Fluent API, I think we do not need to use the method to get the max length any more, we can easily find them in Fluent API.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, December 19, 2013 8:41 AM
    Moderator

All replies

  • Hi Kakaroto,

    I think this thread is specific to Data forum. This form is to discuss problems about CLR development. So I am helping you to move this thread directly into that forum. Thank you for your understanding.

    Best Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, December 19, 2013 6:59 AM
    Moderator
  • Hello,

    >>I just need guidance for getting the maxlenght value but to give some perspective the goal is:

    According to your description of the goal, do you want to create the entity class at runtime?

    If it is, unfortunately, it is impossible.

    If not, in code first, for string type, the default max length is max. Or we can use the Fluent API to specify the max length like below:

    modelBuilder.Entity<Blog>().Property(b => b.Name).HasMaxLength(100);

    Then if we call method using MetadataWorkspace to get the result, it will show 100. My test method is like below:

    public int GetMaxLength<TEntity>(ObjectContext oc, Expression<Func<TEntity, string>> property)
    
                where TEntity : class
    
            {
    
                int MaxLength = 0;
    
                var test = oc.MetadataWorkspace.GetItems(DataSpace.CSpace);
    
    
                if (test == null)
    
                    return -1;
    
    
                Type entType = typeof(TEntity);
    
                string propertyName = ((MemberExpression)property.Body).Member.Name;
    
    
                var q = test
    
                    .Where(m => m.BuiltInTypeKind == BuiltInTypeKind.EntityType)
    
                    .SelectMany(meta => ((EntityType)meta).Properties
    
                    .Where(p => p.Name == propertyName && p.TypeUsage.EdmType.Name == "String"));
    
    
                var queryResult = q.Where(p =>
    
                {
    
                    var match = p.DeclaringType.Name == entType.Name;
    
                    if (!match)
    
                        match = entType.Name == p.DeclaringType.Name;
    
    
                    return match;
    
    
                })
    
                    .Select(sel => sel.TypeUsage.Facets["MaxLength"].Value)
    
                    .ToList();
    
    
                if (queryResult.Any())
    
                {
    
                    string result = queryResult.FirstOrDefault().ToString();
    
                    if (result == "Max")
    
                    {
    
                        MaxLength = -1;
    
                    }
    
                    else
    
                    {
    
                        MaxLength = Convert.ToInt32(result);
    
                    }
    
                }
    
                return MaxLength;
    
            }
    

    However, since we have already specified the max length in Fluent API, I think we do not need to use the method to get the max length any more, we can easily find them in Fluent API.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, December 19, 2013 8:41 AM
    Moderator
  • many thanks for your answer!!!u save my life

    i like “job”

    Friday, August 15, 2014 4:37 PM