我对这个问题很难过。我试图实现的行为是让一个测试分配多个允许的学生组。这可以正常工作,但是当我尝试将同一组分配给多个测试时,它会被移动而不是添加。我认为这些图片解释得最好。
以下是我的模特:
You do not have permission to call X service.
相关的控制器:
public class TestParameters
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
[DefaultValue(60)]
//in minutes
//0 = no limit
[DisplayName("Time Limit")]
public int TimeLimit { get; set; }
[Required]
[DefaultValue(10)]
[DisplayName("Number of Questions")]
public int NumQuestions { get; set; }
[Required]
[DisplayName("Open From")]
public DateTime OpenFrom { get; set; }
[DisplayName("Open To")]
public DateTime OpenTo { get; set; }
[DisplayName("Allowed Groups")]
public virtual ICollection<StudentGroup> AllowedGroups { get; set; }
}
public class StudentGroup
{
[Required]
[Key]
public string Code { get; set; }
[Required]
public string Name { get; set; }
public string Description { get; set; }
}
什么会导致这种行为?
答案 0 :(得分:0)
看起来非常像你设置了一对多关系,而不是你所描述的许多关系。
你在DbContext OnModelCreating:
中需要这样的东西protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<TestParameters>()
.HasMany(x => x.StudentGroup)
.WithMany(x => x.TestParameters)
.Map(x =>
{
x.ToTable("StudentGroupTestParameter")
x.MapLeftKey("Id");
x.MapRightKey("Code");
});
}