Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting mapped column names of properties in entity framework
    text
    copied!<p>in my project I use Entity Framework 6. I have these entities:</p> <pre><code> public class Person { [Key] public int Id { get; set; } public string Name { get; set; } public virtual ICollection&lt;PersonRight&gt; PersonRights { get; set; } } </code></pre> <p>and</p> <pre><code> public class PersonRight { [Key] public int Id { get; set; } public string Name { get; set; } } </code></pre> <p>When I insert a person object with filled in PersonRights it looks like this in the database:</p> <p>table for Person entity:</p> <pre><code>dbo.People with columns Id, Name </code></pre> <p>table for PersonRights entity</p> <pre><code>dbo.PersonRights with columns Id, Name, Person_Id </code></pre> <p>when I load a person from a database it hasnt filled PersonRights property because of the virtual keyword which is enabeling the lazy loading feature - and its okay.</p> <p>Then I get the PersonRights for the person object and it also works fine.</p> <p>The thing is, since there is no navigation property in PersonRight entity, the entity framework must know by which columns in the database are those two properties bounded. In database ther is a foreign key connecting PersonRights and People tables:</p> <pre><code>FK_dbo.PersonRights_dbo.People_Person_Id </code></pre> <p>The question is : Is there any way how to get the column name by which are those two properties connected? Any way how to get the string "Person_Id" in code?</p> <p>There is a way how to find out to which table is an entity bounded in database :</p> <p><a href="http://www.codeproject.com/Articles/350135/Entity-Framework-Get-mapped-table-name-from-an-ent" rel="noreferrer">http://www.codeproject.com/Articles/350135/Entity-Framework-Get-mapped-table-name-from-an-ent</a></p> <p>thanks a lot for your answers :)</p> <p>EDIT:</p> <p>Well I found out that the column name propety is here:</p> <pre><code> var items = ((IObjectContextAdapter)dbContext).ObjectContext.MetadataWorkspace.GetItems(DataSpace.CSSpace); </code></pre> <p>but I still cant reach it, the problem is weird, when I get first item from this collection it shows me that its type is <code>System.Data.Entity.Core.Mapping.StorageEntityContainerMapping</code> but when I go through it by foreach suddenly the type is <code>System.Data.Entity.Metadata.Edm.GlobalItem</code> ...</p> <p>How can I access the <code>System.Data.Entity.Core.Mapping.StorageEntityContainerMapping</code> item where is also the collection I need to get the column named - <code>AssociationSetMappings</code> ??</p>
 

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