我的模型:
public class Person
{
[Key]
public int IdPerson { get; set; }
public string Name { get; set; }
public List<Project> Projects { get; set; }
}
public class Project
{
[Key]
public int IdProject {get; set;}
public string Name { get; set; }
public ICollection<Person> Members { get; set; }
}
我的背景:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Project>()
.HasMany(p => p.Members)
.WithMany(e => e.Projects)
.Map(pe =>
{
pe.ToTable("ProjectPersonTable");
pe.MapLeftKey("IdPerson");
pe.MapRightKey("IdProject");
});
}
现在当我这样做时:
Person person = personRepository.GetSingle(Idperson);
project.Members.Add(person);
将人加倍,我不希望重复或在执行此操作时插入新人。
这是为什么?