Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create association on non-primary key fields with Entity Framework 4.1 Fluent API

We are using EF 4.1 and the fluent API to get data from a legacy database (that we are not permitted to change). We are having a problem creating a relationship between two tables where the related columns are not primary and foreign keys.

With the classes below, how would we configure the one-to-many relationship between Report and RunStat such that Report.RunStats would return all of the RunStat entities where the ReportCode fields are equal?

public class Report
{
    [Key]
    public int ReportKey { get; set; }
    public string Name { get; set; }
    public int ReportCode { get; set; } // Can we associate on this field?
    public virtual ICollection<RunStat> RunStats { get; set; }
}

public class RunStat
{
    [Key]
    public int RunStatKey { get; set; }
    public int ReportCode { get; set; }
    public DateTime RunDate { get; set; }
}

Basically, I want to use the Fluent API to configure EF such that it considers Report.ReportCode to be the foreign key and RunStat.ReportCode to be the primary key.

like image 387
GoClimbColorado Avatar asked Aug 10 '11 23:08

GoClimbColorado


People also ask

How do you configure a primary key for an entity in Entity Framework Fluent API?

Configuring a primary key By convention, a property named Id or <type name>Id will be configured as the primary key of an entity. Owned entity types use different rules to define keys. You can configure a single property to be the primary key of an entity as follows: Data Annotations.

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.

Does Entity Framework require a primary key?

Because Entity Framework is able to modify data in your database it needs a primary key to be defined in order for it to be able to uniquely identify rows.


2 Answers

It is not possible. Relations in EF follows exactly same rules as in the database. It means that principal table must have unique identifier which is referenced by dependent table. In case of database the identifier can be either primary key or unique column(s) of principal table. Otherwise it is not valid relation.

Entity framework doesn't support unique keys. If you want to have one-to-many relation between Report and RunStat your dependent table (RunStat) must contains column with value of Report.ReportKey. There is no other way to make it automatic - otherwise you must simply make it custom property and fill it from entity framework manually when you need it.

like image 191
Ladislav Mrnka Avatar answered Sep 24 '22 13:09

Ladislav Mrnka


This capability is now possible in EF Core 1.0 (EF7) as indicated in the reference provided by @Brian on Jul 16, 2014 to a feature request posted to Microsoft's UserVoice forum.

So that this valuable information is not lost should this information disappear at the referenced page, here is the text of the feature request:

Unique Constraint (i.e. Candidate Key) Support
(posted by Kati Iceva on Sep 10, 2010)

SQL Server and other databases support Unique Constraints on tables. Foreign key constraints are generally based on unique constraints on the principal side, with the Primary Key being only a special case of a unique constraint. The Entity Framework currently only supports basing referential constraints on primary keys and does not have a notion of a unique constraint. The idea is to have:

  • Support for specifying a unique constraint on an Entity
  • Support for specifying a foreign key associations that on the principal end specify columns(s) that comprise a unique constraint but are not the primary key.

And Microsoft's announcement of the implementation of this capability in EF Core 1.0:

Completed
(posted by Diego Vega (Program Manager, Microsoft Entity Framework) on Aug 9, 2016)

Closing as support for this feature was added in EF Core 1.0 and we don’t have plans to add it in the EF6 codebase. Also, please create new ideas for specific improvements to the feature.

like image 30
DavidRR Avatar answered Sep 23 '22 13:09

DavidRR