2016-03-07 22 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看起来和你的建议完全一样。 –

相关问题