0
votes

Introducing FOREIGN KEY constraint 'FK_dbo.Queries_dbo.Users_UserID' on table 'Queries' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint. See previous errors.

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
Please do some minimal effort to state what your issue is, what you tried etc.Alexander Derck
What made you decide to add this single WillCascadeOnDelete call?Gert Arnold

1 Answers

0
votes

Tell EF not to cascade the query delete.

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

Or turn off the convention:

modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();