Print Page | Close Window

FixedLength fields

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2403
Printed Date: 02-Apr-2025 at 4:06am


Topic: FixedLength fields
Posted By: mikewishart
Subject: FixedLength fields
Date Posted: 27-Dec-2010 at 12:14pm
Is there a way to determine if a property has the FixedLength=True set at run time?

<Property Name="StatusCode" Type="String" Nullable="false" MaxLength="6" Unicode="false" FixedLength="true" />




Replies:
Posted By: DenisK
Date Posted: 27-Dec-2010 at 4:52pm
mikewishart;

Unfortunately there is no easy way to do that. What are you trying to achieve?

Here are some suggestions:

1. Read the CSDL content from the edmx.  http://blogs.msdn.com/b/adonet/archive/2008/01/24/how-to-extract-csdl-from-edmx.aspx - http://blogs.msdn.com/b/adonet/archive/2008/01/24/how-to-extract-csdl-from-edmx.aspx

or

2. Write your own custom T4 template for code generation.


Posted By: mikewishart
Date Posted: 27-Dec-2010 at 5:02pm
Hi Dennis,

Thanks for the link.  I'll look at it.  Trying to write a save event handler on the entitymanager which trims leading/trailing characters of all the varchar() fields and Nulls those that support it.  Just a generic safety.  The problem is that some of the fields are char() and shouldn't be trimmed.

for example... right now I have any string[6] hardcoded to not be trimmed...  Not elegant but functional for our application.

    private static void TrimStringProperties(IEnumerable<object> entities)
    {
      foreach (Entity entity in entities)
      {
        var properties = entity.GetType().GetProperties().Where(p => p.PropertyType == typeof(string));
        foreach (var property in properties)
        {
          var attributeNames = property.GetCustomAttributes(false).Select(a => a.ToString()).ToList();
          if (!attributeNames.Any(a => a.Contains("DataMemberAttribute")))
            continue;
          var isNullable = ((IBaseEntity)entity).IsNullable(property.Name);
          var maxLength = ((IBaseEntity)entity).MaxStringLength(property.Name);
          if (maxLength == 6) continue; // fixed length char(6) properties

          var value = (string)property.GetValue(entity, null);
          if (value == null) continue;

          value = value.Trim();
          if (isNullable && string.IsNullOrEmpty(value))
            value = null;
          property.SetValue(entity, value, null);
        }
      }
    }



Posted By: DenisK
Date Posted: 27-Dec-2010 at 7:28pm
I see. 

Another suggestion is to do the trimming on a varchar on the SQL database. You can use SQL_Variant_PROPERTY function to determine the field type and LTrim or RTrim function to do the trimming.




Print Page | Close Window