Reputation: 1602
I have this model:
public class Project
{
public int Id { get; set; }
public int? ParentId { get; set; }
// some more properties...
public List<Project> ChildProjects { get; set; }
}
In the database, the column "ProjectId" is created, which is used to keep track of the relationship to the parent project.
Since "ProjectId" is not part of the entity model, I can't reference it in my queries.
How can I prevent this column from being created, and use "ParentId" instead?
UPDATE
This is my DbContext:
public class ProjectsDbContext : DbContext
{
public ProjectsDbContext(DbContextOptions<ProjectsDbContext> options) : base(options) { }
public DbSet<Project> Projects { get; set; }
}
Upvotes: 2
Views: 225
Reputation: 205539
ProjectId
is the EF Core conventional FK property name - {Entity Name}{Entity PK Property Name}
.
As usual when conventions don't match your model, you have to use explicit configuration with either data annotations or fluent API, for instance in your case
modelBuilder.Entity<Project>()
.HasManye(e => e.ChildProjects)
.WithOne()
.HasForeignKey(e => e.ParentId); // <--
For more info, see the Relationships section of the EF Core documentation.
Upvotes: 2