Sorry, I know this has been asked quite a few times. I have looked at the answers and keep thinking I have understood it only to run smack bang into the same issue every version I try.
First the error:
System.Data.SqlClient.SqlException
Introducing FOREIGN KEY constraint 'FK_Job_WaterBody_WaterBodyID' on table 'Job' 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 or index. See previous errors.
The two tables causing the issues are:
Jobs
public class Job { public int JobID { get; set; } [Required] [Display(Name = "Job Number")] public string JobNumber { get; set; } [Required] [Display(Name = "Site")] public int SiteID { get; set; } [Display(Name = "Water Body")] public int WaterBodyID { get; set; } [Required] [Display(Name = "Client Order")] public string OrderNumber { get; set; } [Required] [Display(Name = "Booking Date")] public DateTime BookingDate { get; set; } [Required] public int DepartmentID { get; set; } [Required] [Display(Name = "Short Job Description")] [StringLength(900, MinimumLength = 20)] public string JobDescription { get; set; } public DateTime? InvoiceDate { get; set; } public Site Site { get; set; } public WaterBody WaterBody { get; set; } }
and Waterbody
public class WaterBody { public int WaterBodyID { get; set; } [Display(Name = "Site")] public int SiteID { get; set; } [Required] [StringLength(50)] [Display(Name = "Water Body Name")] public string WBName { get; set; } [Display(Name = "Location")] public int LocationID { get; set; } [Display(Name = "Pool Type")] public int PoolTypeID { get; set; } [Display(Name = "Construction")] public int ConstructionID { get; set; } public decimal Length { get; set; } public decimal Width { get; set; } public decimal Depth { get; set; } // public ICollection<Site> Site { get; set; } public Site Sites { get; set; } public Construction Construction { get; set; } public Location Location { get; set; } public PoolType PoolType { get; set; } public ICollection<Equipment> Equipment { get; set; } public ICollection<Job> Job { get; set; } [Display(Name = "Volume")] public decimal PoolVolume { get { return Length * Width * Depth; } } [Display(Name = "Area")] public decimal PoolArea { get { return Length * Width; } } }
After reading and thinking I am understanding I have added the following to the OnModelCreating section of Context file:
modelBuilder.Entity<Job>() .HasMany(w => w.WaterBodyID) .WithOptional() .WillCascadeOnDelete(false);
From what I can see this is telling Job to not cascade the waterbody if the job is deleted. I think I have it around the right way. And this is certainly what I want.
However:
HasMany(w=>w.WaterBody)
HasMany(w=>w.Waterbodys)
HasMany(w=>w.WaterBodyID)
Gives me little red squiggles and whilst they are pretty they aren't what I am aiming for. I also played with HasOne in case I misunderstood the way the relationships are, however this gives me squiggles under the HasOne part of the code.
Have I got this round the right way? How do I tell Jobs not to delete the water body if the job is deleted? Am I going about this the right way?
Thanks