Entity Framework - Get Max Length of Column
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Entity Framework - Get Max Length of Column

  1. #1
    Join Date
    Jun 2001
    Location
    Denmark
    Posts
    448

    Question Entity Framework - Get Max Length of Column

    I've been looking for at way to ask EF about the max length of columns to avoid truncation errors.

    I found these two articles:
    http://blog.chronoclast.com/2010/10/...ld-length.html
    and
    http://stackoverflow.com/questions/7...tity-framework
    but I can't get it to work for me. I'm not sure where to get the Context that they talk about.

    Or is there another way?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,217

    Re: Entity Framework - Get Max Length of Column

    Context is the DbContext or ObjectContext.

    How are you using EF? CodeFirst or Designer? Post a code snippet of using your approach to making an EF call (then we can point out the context).

  3. #3
    Join Date
    Jun 2001
    Location
    Denmark
    Posts
    448

    Re: Entity Framework - Get Max Length of Column

    Code:
            public bool VerifyOrderNumber(int OrderNumber)
            {
                Log4netProperties.AddOriginatingServiceMethodToLog4netProperties();
    
                try
                {
                    return entityFramework.RepairOrder.Where(r => r.RepairOrderID == OrderNumber).Count()>0;
                }
                catch (Exception ex)
                {
                    log.Error(ex);
                    entityFramework = new TCXMLIEntities();
                    return false;
                }       
            }
    Here's a snippet from my code

    Thanks for your fast reply :-)

  4. #4
    Join Date
    Jun 2001
    Location
    Denmark
    Posts
    448

    Re: Entity Framework - Get Max Length of Column

    Where
    entityFramework = new TCXMLIEntities();

    and

    public partial class TCXMLIEntities : ObjectContext
    {....}

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,217

    Re: Entity Framework - Get Max Length of Column

    entityFramework is your context. What does this variable's class derive from?

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,217

    Re: Entity Framework - Get Max Length of Column

    Okay. Posting more code while I was answering. Yep, entityFramework is the context.

  7. #7
    Join Date
    Jun 2001
    Location
    Denmark
    Posts
    448

    Re: Entity Framework - Get Max Length of Column

    I tried to add a function looking like this
    Code:
            public int Length<TEntity>(string field)
            {
                var Context = ((IObjectContextAdapter)entityFramework).ObjectContext;
                var item = Context.MetadataWorkspace.GetItem<EntityType>(typeof(TEntity).FullName, DataSpace.CSpace);
                return (int)item.Properties[field].TypeUsage.Facets["MaxLength"].Value;
            }
    but it is not pleased with that cast (to IObjectContextAdapter)

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,217

    Re: Entity Framework - Get Max Length of Column


  9. #9
    Join Date
    Jun 2001
    Location
    Denmark
    Posts
    448

    Re: Entity Framework - Get Max Length of Column

    Okay...this was weird...
    I made this hack to my code:
    Code:
            public int Length<TEntity>(string field)
            {
                var item = /*Context*/entityFramework.MetadataWorkspace.GetItem<EntityType>(typeof(TEntity).FullName.Replace("TCXMLI_DAL", "EfTCXMLI"), DataSpace.CSpace);
                return (int)item.Properties[field].TypeUsage.Facets["MaxLength"].Value;
            }
    Because it claimed that the class repairorder didn't exist in the metadata<something>.

    the class looks like this in the generated code:
    Code:
    namespace TCXMLI_DAL
    {
    
    ....
    
    
      /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmEntityTypeAttribute(NamespaceName="EfTCXMLI", Name="RepairOrder")]
        [Serializable()]
        [DataContractAttribute(IsReference=true)]
        public partial class RepairOrder : EntityObject
        {....}
    
    ....
    
    }
    
    But things appear to work now (or: at least I got something useful back and not a crash!)
    
    Thank you all for your replies. :-)
    
    I don't know how the namespace mismatch happened, but at least now I can continue :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center