entity framework 5 MaxLength

前端 未结 4 1522
灰色年华
灰色年华 2020-12-28 17:47

I was using EF4 and a piece of code I found to get the MaxLength value from an entity like this:

public static int? GetMaxLength(string entityTy         


        
相关标签:
4条回答
  • 2020-12-28 18:22

    I had similar issue and solution is here;

      MyDBEntities ctx = new MyDBEntities();
            var objectContext = ((IObjectContextAdapter)ctx).ObjectContext;
    
            var cols = from meta in objectContext.MetadataWorkspace.GetItems(DataSpace.CSpace)
                       .Where(m => m.BuiltInTypeKind == BuiltInTypeKind.EntityType)
                       from p in (meta as EntityType).Properties
                          .Where(p => p.DeclaringType.Name == "TableName")
                       select new
                       {
                           PropertyName = p.Name                          
                       };
    
    0 讨论(0)
  • 2020-12-28 18:27

    This is a very handy piece of code. I refactored it a bit and it's so useful I thought I would post it here.

    public static int? GetMaxLength<T>(Expression<Func<T, string>> column)
        {
            int? result = null;
            using (var context = new EfContext())
            {
                var entType = typeof(T);
                var columnName = ((MemberExpression) column.Body).Member.Name;
    
                var objectContext = ((IObjectContextAdapter) context).ObjectContext;
                var test = objectContext.MetadataWorkspace.GetItems(DataSpace.CSpace);
    
                if(test == null)
                    return null;
    
                var q = test
                    .Where(m => m.BuiltInTypeKind == BuiltInTypeKind.EntityType)
                    .SelectMany(meta => ((EntityType) meta).Properties
                    .Where(p => p.Name == columnName && 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())
                    result = Convert.ToInt32(queryResult.First());
    
                return result;
            }
        }
    

    And you can call it like:

    GetMaxLength<Customer>(x => x.CustomerName);
    

    This is assuming you've got a DbSet defined in your DbContext of type Customer, which has a property of CustomerName with a defined MaxLength.

    This is very helpful for things like creating Model attributes that set a textbox's maxlength to the max length of the field in the database, always ensuring the two are the same.

    0 讨论(0)
  • 2020-12-28 18:37

    It means that you have not only upgraded EF but you have also changes the API. There are two APIs - the core ObjectContext API and simplified DbContext API. Your code is dependent on ObjectContext API (the only API available in EF4) but EF5 uses DbContext API (added in separate EntityFramework.dll assembly since EF4.1). If you want to use new EF features and your previous code you should just upgrade to .NET 4.5.

    If you also want to use a new API you will have to update a lot of your existing code but it is still possible to get ObjectContext from DbContext and make your method work again. You just need to use this snippet:

    var objectContext = ((IObjectContextAdapter)context).ObjectContext;
    

    and use objectContext instead of context in your code.

    0 讨论(0)
  • 2020-12-28 18:39

    I refactored mccow002's example into a copy-paste-ready Extension method class:

    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    using System.Data.Metadata.Edm;
    
    public static class DbContextExtensions
    {
    
        // get MaxLength as an extension method to the DbContext
        public static int? GetMaxLength<T>(this DbContext context, Expression<Func<T, string>> column)
        {
            return (int?)context.GetFacets<T>(column)["MaxLength"].Value;
        }
    
        // get MaxLength as an extension method to the Facets (I think the extension belongs here)
        public static int? GetMaxLength(this ReadOnlyMetadataCollection<Facet> facets)
        {
            return (int?)facets["MaxLength"].Value;            
        }
    
        // just for fun: get all the facet values as a Dictionary 
        public static Dictionary<string,object> AsDictionary(this ReadOnlyMetadataCollection<Facet> facets) {
            return facets.ToDictionary(o=>o.Name,o=>o.Value);
        }
    
    
        public static ReadOnlyMetadataCollection<Facet> GetFacets<T>(this DbContext context, Expression<Func<T, string>> column)
        {
            ReadOnlyMetadataCollection<Facet> result = null;
    
            var entType = typeof(T);
            var columnName = ((MemberExpression)column.Body).Member.Name;
    
            var objectContext = ((IObjectContextAdapter)context).ObjectContext;
            var test = objectContext.MetadataWorkspace.GetItems(DataSpace.CSpace);
    
            if (test == null)
                return null;
    
            var q = test
                .Where(m => m.BuiltInTypeKind == BuiltInTypeKind.EntityType)
                .SelectMany(meta => ((EntityType)meta).Properties
                .Where(p => p.Name == columnName && 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)
                .FirstOrDefault();
    
            result = queryResult.TypeUsage.Facets;
    
            return result;
    
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题