[英]Introducing Foreign Key Constraint may cause cycles or multiple cascade paths when property is not required
I received this error: 我收到此错误:
Introducing FOREIGN KEY constraint 'FK_dbo.STCIProductInteractiveInfoes_dbo.User_ModifyUserID' on table 'STCIProductInteractiveInfoes' may cause cycles or multiple cascade paths.
在表'STCIProductInteractiveInfoes'上引入FOREIGN KEY约束'FK_dbo.STCIProductInteractiveInfoes_dbo.User_ModifyUserID'可能会导致循环或多个级联路径。 Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。
So I looked it up online (on stackoverflow) and I see that it can be because the foreign key is required. 因此,我在线上(在stackoverflow上)进行了查询,发现这可能是因为需要外键。 I commented out the required property and I am still getting this error.
我注释掉了必需的属性,但仍然出现此错误。
Here is my class: 这是我的课:
namespace PTEManager.Domain
{
public class PTEInteractiveCourse
{
[Key]
public Guid PTEInteractiveCourseId { get; set; }
[ScaffoldColumn(false)]
[Required]
public DateTime ModifyDate { get; set; }
[ScaffoldColumn(false)]
[ForeignKey("ModifyUser")]
//[Required]
public int ModifyUserId { get; set; }
public virtual OpsUser ModifyUser { get; set; }
[Display(Name = "Package")]
[Required]
public int PackageId { get; set; }
[ScaffoldColumn(false)]
[Display(Name = "")]
[Required]
public int Status { get; set; }
[Display(Name = "STCI Course")]
[ForeignKey("STCICourseID")]
[Required]
public Guid STCIProductInteractiveInfoID { get; set; }
public virtual STCIProductInteractiveInfo STCICourseID { get; set; }
}
}
and here is the code that it is crashing on: 这是它崩溃的代码:
[HttpGet]
public ActionResult Index()
{
var model = db.PTEInteractiveCourses
.Where(p => p.Status == 1)
.ToList();
return View(model);
}
When a user deletes an Interactive Course, a user should not be deleted from the user table; 当用户删除交互式课程时,不应从用户表中删除该用户。 just the interactive course should be deleted.
只是互动课程应该被删除。 Which this error is happening on a LINQ query to list the data so I do not even know what deleting it would have to do with it, we will not be adding a delete function anyway.
在LINQ查询上会发生哪个错误以列出数据,所以我什至不知道要删除它有什么关系,无论如何我们都不会添加删除功能。 Well there will be a delete but all it will do is hide the interactive course from the user but it will save the data to prevent data loss.
好吧,将会有一个删除,但是它所要做的就是向用户隐藏交互过程,但是它将保存数据以防止数据丢失。
I have found this code: 我找到了以下代码:
protected override void OnModelCreating( DbModelBuilder modelBuilder )
{
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
}
which should theoretically work because I do not need any cascading deletes for this project but where would I put this code? 从理论上讲这应该可行,因为我不需要此项目的任何级联删除,但是我将把这些代码放在哪里? It says in my datacontext class but when I do I have a build error on the void keyword.
它在我的datacontext类中说,但是当我这样做时,void关键字上出现构建错误。 Here is my datacontext class:
这是我的datacontext类:
namespace PTEManager.Domain.Data
{
public class DataContext : DbContext, IDataContext
{
public DataContext()
: base("HelpDesk")
{
}
//public DataContext(DbConnection existingConnection, bool contextOwnsConnection)
// : base(existingConnection, contextOwnsConnection)
//{
//}
/// <summary>
/// any entity to be persisted must be declared here.
/// </summary>
///
public DbSet<OpsUser> OpsUsers { get; set; }
public DbSet<PTEInteractiveCourse> PTEInteractiveCourses { get; set; }
public DbSet<PTETrackingClass> PTETrackingClasses { get; set; }
public DbSet<STCIProductInteractiveInfo> STCIProductInteractiveInfos { get; set; }
}
}
OpsUser Class: OpsUser类别:
namespace PTEManager.Domain
{
[Table("User")]
public class OpsUser
{
[Key]
public int u_user_id { get; set; }
public Guid DepartmentID { get; set; }
[MaxLength(50)]
[Required]
public string email_addr { get; set; }
[MaxLength(30)]
[Required]
public string first_nme { get; set; }
[MaxLength(30)]
[Required]
public string last_nme { get; set; }
[Required]
public Guid msrepl_tran_version { get; set; }
[MaxLength(1)]
[Required]
public string status { get; set; }
[MaxLength(15)]
[Required]
public string user_nme { get; set; }
[Required]
public int u_branch_id { get; set; }
}
}
PTETrackingClass: PTETrackingClass:
namespace PTEManager.Domain
{
public class PTETrackingClass
{
[Key]
public Guid PTETrackingClassId { get; set; }
[ScaffoldColumn(false)]
[Required]
public DateTime ModifyDate { get; set; }
[ScaffoldColumn(false)]
[ForeignKey("ModifyUser")]
[Required]
public int ModifyUserId { get; set; }
public virtual OpsUser ModifyUser { get; set; }
[Display(Name = "Extensions Allowed")]
[Required]
public int NumberOfExtensions { get; set; }
[ScaffoldColumn(false)]
[Required]
public int Status { get; set; }
[Display(Name="Tracking Class Code")]
[Required]
public Guid TrackingClassCode { get; set; }
}
}
STCIProductInteractiveInfo: STCIProductInteractiveInfo:
namespace PTEManager.Domain
{
public class STCIProductInteractiveInfo
{
[Key]
public Guid STCIProductInteractiveInfoID { get; set; }
public Guid MarketingTextID { get; set; }
[Required]
public DateTime ModifyDate { get; set; }
[ForeignKey("ModifyUser")]
//[Required]
public int ModifyUserID { get; set; }
public virtual OpsUser ModifyUser { get; set; }
[MaxLength(50)]
[Required]
public string STCICourseID { get; set; }
[MaxLength(100)]
[Required]
public string STCICourseName { get; set; }
public byte STCIProductEmailHTML { get; set; }
[MaxLength(255)]
public string STCIProductEmailHTMLDateType { get; set; }
public int STCIProductEmailHTMLFileLength { get; set; }
public byte STCIProductEmailText { get; set; }
[MaxLength(255)]
public string STCIProductEmailTextDataType { get; set; }
public string STCIProductEmailTextFileLength { get; set; }
[MaxLength(50)]
[Required]
public string STCITrackingClassCode { get; set; }
[MaxLength(100)]
[Required]
public string STCITrackingClassName { get; set; }
[Required]
public int u_product_id { get; set; }
}
}
You have a circular relation on your Tables to resolve you have to option: 您的表上有一个循环关系,以解决必须选择的问题:
1) If User is optional in STCIProductInteractiveInfo
or PTEInteractiveCourse
you could simply change 1)如果用户在
STCIProductInteractiveInfo
或PTEInteractiveCourse
是可选的, STCIProductInteractiveInfo
只需更改
public class STCIProductInteractiveInfo // or PTEInteractiveCourse
{
[Required]
public int ModifyUserID { get; set; }
}
to 至
public class STCIProductInteractiveInfo // or PTEInteractiveCourse
{
// [Required] remove required attribute too
public int? ModifyUserID { get; set; }
}
2) But if you want keep User Required
simply add this on your DbContext 2)但是如果您想保留User
Required
只需在DbContext上添加它即可
public class DataContext : DbContext, IDataContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<STCIProductInteractiveInfo>()
.HasRequired(e => e.ModifyUser)
.WithMany()
.WillCascadeOnDelete(false);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.