Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After a peek into a entity framework model designer I saw that it uses the <code>EdmEntityTypeAttribute</code> and <code>DataMemberAttribute</code> to decorate generated classes and properties. Each of them have a <code>Name</code> property which contains the name of the mapped entity (table, column respectively). When the property name matches the name of the column, the designer does not supply a value for positional argument <code>Name</code>. The code below works fine for me.</p> <pre><code> private static string GetTableName&lt;T&gt;() where T : EntityObject { Type type = typeof(T); var at = GetAttribute&lt;EdmEntityTypeAttribute&gt;(type); return at.Name; } private static string GetColumnName&lt;T&gt;(Expression&lt;Func&lt;T, object&gt;&gt; propertySelector) where T : EntityObject { Contract.Requires(propertySelector != null, "propertySelector is null."); PropertyInfo propertyInfo = GetPropertyInfo(propertySelector.Body); DataMemberAttribute attribute = GetAttribute&lt;DataMemberAttribute&gt;(propertyInfo); if (String.IsNullOrEmpty(attribute.Name)) { return propertyInfo.Name; } return attribute.Name; } private static T GetAttribute&lt;T&gt;(MemberInfo memberInfo) where T : class { Contract.Requires(memberInfo != null, "memberInfo is null."); Contract.Ensures(Contract.Result&lt;T&gt;() != null); object[] customAttributes = memberInfo.GetCustomAttributes(typeof(T), false); T attribute = customAttributes.Where(a =&gt; a is T).First() as T; return attribute; } private static PropertyInfo GetPropertyInfo(Expression propertySelector) { Contract.Requires(propertySelector != null, "propertySelector is null."); MemberExpression memberExpression = propertySelector as MemberExpression; if (memberExpression == null) { UnaryExpression unaryExpression = propertySelector as UnaryExpression; if (unaryExpression != null &amp;&amp; unaryExpression.NodeType == ExpressionType.Convert) { memberExpression = unaryExpression.Operand as MemberExpression; } } if (memberExpression != null &amp;&amp; memberExpression.Member.MemberType == MemberTypes.Property) { return memberExpression.Member as PropertyInfo; } throw new ArgumentException("No property reference was found.", "propertySelector"); } // Invocation example private static Test() { string table = GetTableName&lt;User&gt;(); string column = GetColumnName&lt;User&gt;(u=&gt;u.Name); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload