我有这两个实体:
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public bool? IsActive { get; set; }
public bool? IsAdmin { get; set; }
public bool? DeletedFlag { get; set; }
public DateTime LastLoginDate { get; set; }
public DateTime CreatedDate { get; set; }
public virtual ICollection<Vote> VoterUser { get; set; }
public virtual ICollection<Vote> VotedUser { get; set; }
}
public class Vote
{
public int Id { get; set; }
public DateTime VoteDate { get; set; }
public int Rate { get; set; }
public int UserId { get; set; }
public int VoterId { get; set; }
public User VotedUser { get; set; }
public User VoterUser { get; set; }
}
并在流利的 api 中定义了这样的外键:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>().HasKey(x => x.Id).Property(x => x.Id).IsRequired();
modelBuilder.Entity<User>().HasMany(x => x.VotedUser).WithRequired(q => q.VotedUser).HasForeignKey(
q => new {q.UserId});
modelBuilder.Entity<User>().HasMany(x => x.VoterUser).WithRequired(q => q.VoterUser).HasForeignKey(
q => new {q.VoterId});
modelBuilder.Entity<Vote>().HasKey(x => x.Id).Property(x => x.Id).IsRequired();
modelBuilder.Entity<Vote>().HasRequired(x => x.VotedUser).WithMany().HasForeignKey(p => p.UserId);
modelBuilder.Entity<Vote>().HasRequired(x => x.VoterUser).WithMany().HasForeignKey(p => p.VoterId);
base.OnModelCreating(modelBuilder);
}
但是当我尝试创建我得到的数据库时;
“在表 'Votes' 上引入 FOREIGN KEY 约束 'FK_Votes_Users_UserId' 可能会导致循环或多个级联路径。指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。无法创建约束。请参阅以前的错误。”
我在哪里犯了错误?
谢谢