2016-03-07 51 views
0

引入表'查詢'上的FOREIGN KEY約束'FK_dbo.Queries_dbo.Users_UserID'可能導致週期或多個級聯路徑。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY約束。 無法創建約束。查看以前的錯誤。EF6代碼第一關係可能導致週期或多個級聯路徑

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); 

    } 

} 
+0

請做一些最小的努力來說明你的問題是什麼,你嘗試過什麼等 –

+0

是什麼讓你決定加入這個單一'WillCascadeOnDelete'打電話? –

回答

0

告訴EF不要級聯查詢刪除。

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

或關閉約定:

modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>(); 
+0

看看OP發佈代碼的末尾('OnModelCreating')。 IMO看起來和你的建議完全一樣。 –

相關問題