我设置了这些模型:
public class Advisor
{
public virtual int AdvisorId { get; set; }
public virtual int UserId { get; set; }
public User User { get; set; }
public ICollection<AdvisorStudentMap> AdvisorStudentMaps { get; set; }
}
public class AdvisorStudentMap
{
[Required]
public virtual int AdvisorId { get; set; }
[Required]
public virtual int UserId { get; set; }
}
public class User
{
public virtual int UserId { get; set; }
public virtual string UserName { get; set; }
public virtual string FirstName { get; set; }
public ICollection<AdvisorStudentMap> AdvisorStudentMaps { get; set; }
}
在我的 OnModelCreating 我有:
modelBuilder.Entity<AdvisorStudentMap>()
.HasKey(t => new {t.AdvisorId, t.UserId});
在我的流利的 api 中,如何设置它,以便当我删除顾问时,它也会删除 AdvisorStudentMap?我不断收到错误消息:表 'AdvisorStudentMaps' 上的 Message=Introducing FOREIGN KEY 约束 'FK_dbo.AdvisorStudentMaps_dbo.Users_UserId' 可能会导致循环或多个级联路径。指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。无法创建约束。请参阅以前的错误。
更新-也在 OnModelCreating 我有:
modelBuilder.Entity<Advisor>()
.HasRequired(t => t.AdvisorStudentMaps).WithMany().WillCascadeOnDelete(true);
有了这个我得到错误'级联外键'FK_dbo.Advisors_dbo.AdvisorStudentMaps_AdvisorId_UserId'不能创建引用列'Advisors.AdvisorId'是一个标识列。