Consider the following database tables. Unfortunately, tables cannot be modified in any way.

Houses has an auto-increment identifier field named Id , a string field named Name and an integer field named AreaId . The latter is not a foreign key of the Areas table.
Areas has a composite key consisting of AreaId , CountryId and LangId . An area with the same AreaId may exist, but with different CountryId and LangId . For example: there may be two lines with the same AreaId , but different LangId .
NOTE. Why does House have several Area s? A House does not have several Area's, it only has one Area . The . The area table has a composite key, which means that a particular row will have multiple translations. For example: Area 5 identifier may 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 aggregate key is defined in context, as indicated in the documents.
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Area>() .HasKey(a => new { a.AreaId, a.CountryId, a.LangId }); }
For example, you can get a list of all the Houses in the database, including their respective fields.
_context.Houses.Include(h => h.Areas).ToList();
The following SQL is created in the output window, and the resulting list contains houses that incorrectly match 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 associates [a].[AreaId] with [h].[Id] , not [h].[AreaId] . How can I express this attitude in EF?