C # code (first EF6 code):
public class ProjectSourceProject
{
[Key, Column(Order = 0)]
public int ProjectSource_id { get; set; }
[Key, Column(Order = 1)]
public int Project_id { get; set; }
public ProjectSourceProject()
{
}
}
Database:

One or more validation errors were detected during model generation:
EntityType 'ProjectSourceProject' has no key defined. Define the key for this EntityType.
C # code 2:
public class ProjectSourceProject
{
public int ProjectSource_id { get; set; }
public int Project_id { get; set; }
public ProjectSourceProject()
{
}
}
and
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ProjectSourceProject>()
.HasKey(psp => new { psp.ProjectSource_id, psp.Project_id });
}
The following strange error appeared:
The properties expression 'psp => new <>f__AnonymousType0`2(ProjectSource_id = psp.ProjectSource_id, Project_id = psp.Project_id)' is not valid. The expression should represent a property: C
Edited: Problem fixed, thanks Moho :)
This works in both scenarios.
source
share