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!
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.
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.
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.
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.
Entity framework demands that relation is built between whole primary key in the principal table and corresponding columns (foreign key) it the dependent table.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With