【发布时间】:2019-06-12 17:01:07
【问题描述】:
我将创建应用程序来保存健身结果。 我有课练习:
public class Exercise
{
public Guid Id { get; protected set; }
public string Name { get; protected set; }
public Category Category { get; protected set; }
}
以及包含练习列表的培训计划:
public class TrainingPlan
{
public Guid Id { get; protected set; }
public string Name { get; protected set; }
public IEnumerable<Exercise> Exercises { get; protected set; }
}
我创建 EntityFramework DbContext:
public class GymContext : DbContext
{
public GymContext(DbContextOptions<GymContext> options) : base(options) { }
public DbSet<Exercise> Exercises { get; set; }
public DbSet<TrainingPlan> TrainingPlans { get; set; }
}
然后我使用了命令 add-migration 和 update-database。对于带有练习的表,EF 添加了带有 TrainingPlanId 的附加字段。所以现在我只能将锻炼分配给一个 TrainingPlan。但是我想为一些计划分配锻炼,这种情况的最佳解决方案是什么?
【问题讨论】:
-
看起来你想要一个多对多的关系。请看这个网站:entityframeworktutorial.net/code-first/…
-
你是用ef core还是ef开发的? ef core,尽量参考Many-to-many
标签: c# .net entity-framework asp.net-core