menu

arrow_back How to properly set up connections in Entity Framework Core?

by
1 vote
There are two models:
[Table("params")]
public class ParamModel {
[Key]
[Column("id")]
public int Id {
get; set;
}
[Column("name")]
public string Name {
get; set;
}
[Column("value")]
public string Value {
get; set;
}
[Column("algorithm")]
public int AlgorithmID {
get; set;
}
public AlgorithmModel Algorithm {
get; set;
}
}
[Table("algorithms")]
public class AlgorithmModel {
[Key]
[Column("id")]
public int Id {
get; set;
}
[Column("name")]
public string Name {
get; set;
}
public List<ParamModel> Params {
get; set;
}
}
The connections are constructed in this way:
modelBuilder.Entity<AlgorithmModel>().HasMany(p => p.Params).WithOne(p => p.Algorithm).
HasForeignKey(p => p.AlgorithmID);
modelBuilder.Entity<ParamModel>().HasOne(p => p.Algorithm).WithMany(p => p.Params).
HasForeignKey(p=>p.AlgorithmID);
But I get null when I read the Params property from the AlgorithmModel. (The base is full) How to fix it?

2 Comments

Vasily Bannikov , thank you. That solved the problem.
Include wrote?

1 Answer

by
 
Best answer
0 votes
It's not done very nicely. The [Column("name")] attributes are superfluous. I understand that one algorithm has several parameters, so I would add the ForignKey attribute to the AlgorithmID and the virtual modifier to the List list