During the editing of ContractTypeOne and ContractTypeTwo models I received an error.
Error:
Introducing FOREIGN KEY constraint ‘FK_ContractTypeTwos_ContractTypeOnes_ContractTypeOneId’ on table ‘ContractTypeTwos’ 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.
I would like the ContractTypeTwo to be removed after removing ContractTypeOne. But I don’t want to delete Contract after removing ContractTypeOne.
namespace ContractAppAPI.Models
{
public class Contract
{
public int Id { get; set; }
public int ContractNumber { get; set; }
public string Name { get; set; }
public DateTime DateOfConclusion { get; set; }
public string Description { get; set; }
public decimal Value { get; set; }
public string Contractor { get; set; }
public string Signatory { get; set; }
public Boolean HasPdf { get; set; }
public ContractTypeOne ContractTypeOne { get; set; }
public ContractTypeTwo ContractTypeTwo { get; set; }
public ICollection<ContractPdf> ContractPdfs { get; set; }
public ICollection<AnnexToTheContract> AnnexToTheContracts { get; set; }
}
}
namespace ContractAppAPI.Models
{
public class ContractTypeOne
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Contract> Contracts { get; set; }
public ICollection<ContractTypeTwo> ContractTypeTwos { get; set; }
}
}
namespace ContractAppAPI.Models
{
public class ContractTypeTwo
{
public int Id { get; set; }
public string Name { get; set; }
public int ContractTypeOneId { get; set; }
public ContractTypeOne ContractTypeOne { get; set; }
public ICollection<Contract> Contracts { get; set; }
}
}
modelBuilder.Entity<ContractTypeOne>()
.HasMany(cto => cto.ContractTypeTwos)
.WithOne(ctt => ctt.ContractTypeOne)
.HasForeignKey(ctt => ctt.ContractTypeOneId)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<ContractTypeTwo>()
.HasOne(ctt => ctt.ContractTypeOne)
.WithMany(cto => cto.ContractTypeTwos)
.HasForeignKey(ctt => ctt.ContractTypeOneId)
.OnDelete(DeleteBehavior.NoAction);
Bartek S is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.