Note that there are some explanatory texts on larger screens.

plurals
  1. POHow To Change Navigation Property Names Into Meaningful Names
    primarykey
    data
    text
    <p>I'm using Database First with Entity Framework 5. We have two tables (massively simplified):</p> <ul> <li>Addresses <ul> <li>Street</li> <li>Town (etc.)</li> </ul></li> <li>Customers <ul> <li>Name</li> <li>BillingAddress</li> <li>DeliveryAddress</li> <li>AltDeliveryAddress</li> </ul></li> </ul> <p>When we use Visual Studio to import the database into EF ("Update Model from Database"), we end up with code like this:</p> <pre><code>Customer myCustomer; var a = myCustomer.Address; var b = myCustomer.Address1; var c = myCustomer.Address2; </code></pre> <p>What I want obviously is something like this:</p> <pre><code>var a = myCustomer.BillingAddress; var z = myCustomer.BillingAddress.Street; // etc. </code></pre> <p>I could just edit the model in the designer, changing the Navigation Property to give me the correct name. However this isn't a viable solution because we rebuild the model every time we make changes to the database.</p> <p>One option I've tried is creating a partial class like this (code copied from existing MyModel.Designer.cs with just the property name changed):</p> <pre><code>public partial class Customer : EntityObject { [EdmRelationshipNavigationPropertyAttribute("MyModel", "FK_Customers_Addresses_BillingAddress", "Address")] public Address BillingAddress { get { return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference&lt;LookupItem&gt;("MyModel.FK_Customers_Addresses_BillingAddress", "Address").Value; } set { ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference&lt;LookupItem&gt;("MyModel.FK_Customers_Addresses_BillingAddress", "Address").Value = value; } } } </code></pre> <p>However when I run this, I get the following error:</p> <blockquote> <p>The number of members in the conceptual type 'MyModel.Customer' does not match with the number of members on the object side type 'MyNamespace.DataModel.Customer'. Make sure the number of members are the same.</p> </blockquote> <p>I've tried using the [NotMapped()] attribute, but that hasn't made any difference. If I remove the [EdmRelationshipNavigationPropertyAttribute...] attribute then the Linq complains with the following error:</p> <blockquote> <p>The specified type member 'BillingAddress' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.</p> </blockquote> <p>Is there any other way to achieve meaningful names in the Customers object?</p> <p>Here's what I want at the end:</p> <pre><code>var j = myCustomer.BillingAddress; var k = myCustomer.BillingAddress.Street; var l = myCustomer.BillingAddress.Town; // etc. </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.
 

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