首页 文章

EF6 Code First -relationship可能会导致循环或多个级联路径

提问于
浏览
0

在表'查询'上引入FOREIGN KEY约束'FK_dbo.Queries_dbo.Users_UserID'可能会导致循环或多个级联路径 . 指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束 . 无法创建约束 . 查看以前的错误 .

public partial class User
{
    public User()
    {
        this.Alerts = new HashSet<Alert>();
        this.DeviceTokens = new HashSet<DeviceToken>();
        this.MobileNotifications = new HashSet<MobileNotification>();
        this.Queries = new HashSet<Query>();
        this.SendQueries = new HashSet<SendQuery>();
    }

    public int ID { get; set; }
    public string Email { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string SSOID { get; set; }
    public Nullable<System.DateTime> LastLogin { get; set; }
    public int LatestUpdatedRecord { get; set; }

    public virtual ICollection<Alert> Alerts { get; set; }
    public virtual ICollection<DeviceToken> DeviceTokens { get; set; }
    public virtual ICollection<MobileNotification> MobileNotifications { get; set; }
    public virtual ICollection<Query> Queries { get; set; }
    public virtual ICollection<SendQuery> SendQueries { get; set; }
}









public partial class Query
{
    public Query()
    {
        this.AlertEmails = new HashSet<AlertEmail>();
        this.Alerts = new HashSet<Alert>();
        this.QueryFacets = new HashSet<QueryFacet>();
    }

    public int ID { get; set; }
    public int UserID { get; set; }
    public string EntityType { get; set; }
    public string Name { get; set; }
    public string SearchTerm { get; set; }
    public string OrderBy { get; set; }
    public string QueryType { get; set; }
    public string ReceiveUpdateTime { get; set; }
    public Nullable<System.DateTime> NextSendTime { get; set; }
    public bool IsActive { get; set; }
    public string Token { get; set; }
    public string AlertName { get; set; }
    public bool Enabled { get; set; }
    public bool GetNotifications { get; set; }
    public string TimeFilterType { get; set; }
    public string TimeFilterValue { get; set; }
    public string RectangleFilter { get; set; }

    public virtual ICollection<AlertEmail> AlertEmails { get; set; }
    public virtual ICollection<Alert> Alerts { get; set; }
    public virtual ICollection<QueryFacet> QueryFacets { get; set; }
    public virtual User User { get; set; }
}




public partial class SearchAndAlertDbContext : DbContext
{

    public virtual DbSet<AlertEmail> AlertEmails { get; set; }
    public virtual DbSet<AlertingTime> AlertingTimes { get; set; }
    public virtual DbSet<Alert> Alerts { get; set; }
    public virtual DbSet<DeviceToken> DeviceTokens { get; set; }
    public virtual DbSet<IgnoredSlide> IgnoredSlides { get; set; }
    public virtual DbSet<Log> Logs { get; set; }
    public virtual DbSet<MobileNotification> MobileNotifications { get; set; }
    public virtual DbSet<Query> Queries { get; set; }
    public virtual DbSet<QueryFacet> QueryFacets { get; set; }
    public virtual DbSet<SendQuery> SendQueries { get; set; }
    public virtual DbSet<StoredQuery> StoredQueries { get; set; }
    public virtual DbSet<User> Users { get; set; }
    public virtual DbSet<BlockedUserForActivity> BlockedUserForActivities { get; set; }
    public virtual DbSet<UserActivity> UserActivities { get; set; }
    public virtual DbSet<UserActivityIgnoreList> UserActivityIgnoreLists { get; set; }
    public virtual DbSet<UserActivityMonitor> UserActivityMonitors { get; set; }
    public virtual DbSet<UserActivitySpecificSetting> UserActivitySpecificSettings { get; set; }
    public virtual DbSet<WarnedUserForActivity> WarnedUserForActivities { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>().
            HasMany(p => p.Queries).
            WithRequired(a => a.User).
            HasForeignKey(a => a.UserID).WillCascadeOnDelete(false);

        base.OnModelCreating(modelBuilder);

    }

}

1 回答

  • 0

    告诉EF不要级联查询删除 .

    modelBuilder.Entity<Query>()
                .HasRequired(q => q.User)
                .WithMany(s => s.Queries)
                .HasForeignKey(s => s.UserId)
                .WillCascadeOnDelete(false);
    

    或者关闭惯例:

    modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
    

相关问题