Consider the following database tables. Unfortunately the tables cannot be altered in any way.
Houses
has an auto-increment ID field named Id
, a string field named Name
and an integer field named AreaId
. The latter is not a foreign key to the Areas
table.
Areas
has a composite key consisting of AreaId
, CountryId
and LangId
. An Area with the same AreaId
can exist but with different CountryId
and LangId
. E.g.: There can be two rows with the same AreaId
but different LangId
.
NOTE: Why does a House
have multiple Area
s? A House
doesn't have multiple Area's, it only has one
Area. The
Area`s table has a composite key, meaning that a specific row will have multiple translations. E.g.: Area ID 5 might have LangId 5 for English and LangId 3 for Spanish.
The two tables are described by the following two C# classes.
public class House
{
public int Id { get; set; }
[MaxLength(80)]
public string Name { get; set; }
public int? AreaId { get; set; }
[ForeignKey("AreaId")]
public List<Area> Areas { get; set; }
}
public class Area
{
public int AreaId { get; set; }
public int CountryId { get; set; }
public string LangId { get; set; }
public string Name { get; set; }
}
The composite key is defined in the context, exactly as stated in the docs.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Area>()
.HasKey(a => new { a.AreaId, a.CountryId, a.LangId });
}
For example let's get a list of all the Houses in the database, including their respective areas.
_context.Houses.Include(h => h.Areas).ToList();
The following SQL is generated in the output window and the resulting List contains Houses incorrectly matched with the Areas.
SELECT [a].[AreaId], [a].[CountryId], [a].[LangId], [a].[Name]
FROM [Areas] AS [a]
WHERE EXISTS (
SELECT 1
FROM [Houses] AS [h]
WHERE [a].[AreaId] = [h].[Id])
ORDER BY [a].[Id]
As you can see, EntityFramework relates [a].[AreaId]
with [h].[Id]
and not [h].[AreaId]
. How can I express this relationship in EF?