I received this error:
Introducing FOREIGN KEY constraint 'FK_dbo.STCIProductInteractiveInfoes_dbo.User_ModifyUserID' on table 'STCIProductInteractiveInfoes' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
So I looked it up online (on stackoverflow) and I see that it can be because the foreign key is required. 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. 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. Here is my datacontext class:
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:
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:
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:
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
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
public class DataContext : DbContext, IDataContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<STCIProductInteractiveInfo>()
.HasRequired(e => e.ModifyUser)
.WithMany()
.WillCascadeOnDelete(false);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.