Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity Framework 4.1 Dynamically retrieve mapped column name
    primarykey
    data
    text
    <p>I am trying to construct an SQL statement dynamically.</p> <p>My context is created dynamically, using reflection finding classes deriving from EntityTypeConfiguration and adding them to DbModelBuilder.Configuration.</p> <p>My EntityTypeConfiguration classes specify HasColumnName to map the Entity property name to db table column name, which I need to construct my SQL statement.</p> <pre><code>namespace MyDomain { public class TestEntityConfig : EntityTypeConfiguration&lt;TestEntity&gt;{ Property("Name").HasColumnName("dbName"); } } </code></pre> <p>From What I have researched, it seems I can get access to this information through MetadataWorkspace, which I can get to through ObjectContext. </p> <p>I have managed to retrieve the the entity I am interested in with MetadataWorkspace.GetItem("MyDomain.TestEntity",DataSpace.OSpace), which gives me access to Properties, but none of the properties, of Properties, give me the name of the mapped db column, as specified with HasColumnName.</p> <p>Also I am not clear what DataSpace.OSpace is and why my model is constructed in this space.</p> <p>If Anyone can shed some light on this I would be grateful</p> <h2>UPDATE</h2> <p>Further to @Ladislav's comments. I discovered I can get the information as follows For the class properties </p> <pre><code>ctx.MetadataWorkspace.GetItem&lt;ClrEntityType&gt;("MyDomain.TestEntity", DataSpace.OSpace)).Members </code></pre> <p>For the table properties</p> <pre><code>ctx.MetadataWorkspace.GetItem&lt;EntityType&gt;("CodeFirstDatabaseSchema.TestEntity",SSpace).Members </code></pre> <p>So given that I only know the type MyDomain.TestEntity and Memeber "Name". How would I go about to get "dbName". Can I always assume that my mapped class will be created in CodeFirstDatabaseSchema, om order to dynamically construct the identity to retrieve it from SSpace and how would I get to the correct Member in SSpace. Can I do something like</p> <pre><code>var memIndex = ctx.MetadataWorkspace.GetItem&lt;ClrEntityType&gt;("MyDomain.TestEntity", DataSpace.OSpace)).Members["Name"].Index; var dbName = ctx.MetadataWorkspace.GetItem&lt;EntityType&gt;("CodeFirstDatabaseSchema.TestEntity",SSpace).Members[memIndex]; </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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