Hi, I have an MVC 6 application with RTM and Entity Framework Core 1.0 SQL Server, when I try to create database tables, I got the message "Representing a FOREIGN KEY constraint can lead to loop or multiple cascading path errors."
My model classes are as follows:
Category Model:
public class Category
{
public Category()
{
this.VariableSettings = new List<VariableSetting>();
}
public int CategoryId { get; set; }
public string CategoryName { get; set; }
public List<VariableSetting> VariableSettings { get; set; }
public List<Station> Stations { get; set; }
}
Station Model:
public class Station
{
public int StationId { get; set; }
public string StationName { get; set; }
public double? Longitude { get; set; }
public double? Latitude { get; set; }
public List<VariableRecord> VariableRecords { get; set; }
public int CategoryID { get; set; }
public Category Category { get; set; }
}
VariableRecord Model:
public class VariableRecord
{
[Key]
public int VariableRecordId { get; set; }
public double Value { get; set; }
public DateTime RecordDate { get; set; }
public int StationId { get; set; }
public Station Station { get; set; }
public int VarSettingId{ get; set; }
public virtual VariableSetting VariableSetting { get; set; }
}
VariableSetting Model:
public class VariableSetting
{
[Key]
public int VarSettingId { get; set; }
public int Sequence { get; set; }
public double? MinimumValue { get; set; }
public double? MaximumValue { get; set; }
public int CategoryId { get; set; }
public virtual Category Category { get; set; }
public int VariableId { get; set; }
public Variable Variable { get; set; }
public List<VariableRecord> VariableRecords { get; set; }
}
Variable Model:
public class Variable
{
public int VariableId { get; set; }
public string VariableName { get; set; }
public string Description { get; set; }
public string Unit { get; set; }
public List<VariableSetting> VariableSettings { get; set; }
}
So, in my codes there are also Cascade Category-> Station-> VariableRecord routes and Category-> VariableSetting-> VariableRecord routes, so I'm trying to redirect the Category-> VariableSetting-> VariableRecord route to the context class using the Fluent API, as shown below:
builder.Entity<VariableRecord>()
.HasOne(pt => pt.VariableSetting)
.WithMany(p => p.VariableRecords)
.HasForeignKey(pt => pt.VarSettingId)
.OnDelete(DeleteBehavior.Cascade);
However, I still got the same as below:
System.Data.SqlClient.SqlException(0x80131904): FOREIGN KEY 'FK_VariableSetting_Category_CategoryId' "VariableSetting" . ON DELETE NO ACTION ON UPDATE NO ACTION FOREIGN KEY.
, , , !