Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework 4.1 - Relationships between non-key columns

I have 2 entities that are related, but the legacy sql schema essentially has 2 key columns for the same table (not a 2-column key: see below). I need to create a relationship back to the 'faux key' column. Is there a way to do this declaratively in Entity Framework 4.1?

Public Class Client
    Inherits ModelBase

    <Key(), Required()>
    Public Property ClientID As Decimal

    <Required(), StringLength(50)>
    Public Property ClientCode As String

    ........


Public Class ClientLocation
    Inherits ModelBase

    ........

    <Required(), StringLength(50)>
    Public Property ClientCode As String

    ........

    <ForeignKey("ClientCode")>
    Public Overridable Property Client As Clients.Client

And the error I am getting is:

*One or more validation errors were detected during model generation: System.Data.Edm.EdmAssociationConstraint: : The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role. The type of property 'ClientCode' on entity 'ClientLocation' does not match the type of property 'ClientID' on entity 'Client' in the referential constraint 'ClientLocation_Client'.*

Because it thinks I'm trying to map ClientLocation.ClientCode > Client.ClientID, when I am really trying to map ClientLocation.ClientCode > Client.ClientCode...

Any thoughts?

Thanks!

like image 234
Tom Halladay Avatar asked Aug 24 '11 17:08

Tom Halladay


People also ask

What are non key columns?

Nonkey columns: columns added to the INCLUDE clause of a nonclustered index. The basic syntax to create a nonclustered index with nonkey columns is: CREATE INDEX Index_Name ON Schema.TableName(Column) INCLUDE (ColumnA, ColumnB); 1. CREATE INDEX Index_Name ON Schema.

How do you set relationships in EF core?

The easiest way to configure a one-to-many relationship is by convention. EF Core will create a relationship if an entity contains a navigation property. Therefore, the minimum required for a relationship is the presence of a navigation property in the principal entity: public class Author.

How do I add a foreign key in fluent API?

You can then configure foreign key properties by using the HasForeignKey method. This method takes a lambda expression that represents the property to be used as the foreign key.

What is navigation property in entity Framework?

A navigation property is an optional property on an entity type that allows for navigation from one end of an association to the other end. Unlike other properties, navigation properties do not carry data.


1 Answers

Entity framework demands that relation is built between whole primary key in the principal table and corresponding columns (foreign key) it the dependent table.

like image 102
Ladislav Mrnka Avatar answered Oct 22 '22 22:10

Ladislav Mrnka