Answer the question
In order to leave comments, you need to log in
How to properly set up relationships in Entity Framework Core?
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;
}
}
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);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question