0

I have a problem when I want to create my SQL Server database from my EF model.

Here's my model :

public class BilanPatrimonial
{
    public int ID { get; set; }
    
    [Key, ForeignKey("EtatCivil")]
    public int EtatCivilMonsieurID { get; set; }
    public EtatCivil EtatCivilMonsieur { get; set; }

    public int EtatCivilMadameID { get; set; }
    public EtatCivil EtatCivilMadame { get; set; }
 }

public class EtatCivil
{
    public int ID { get; set; }

    public string Prenoms { get; set; }

    public DateTime DateNaissance { get; set; }

    public string LieuNaissance { get; set; }

    public string Nationalite { get; set; }

    public StatutProfessionnel StatutProfessionnel { get; set; }

    public string Profession { get; set; }

    public string NomEmployeur { get; set; }

    public string Activité { get; set; }

    public int Ancienneté { get; set; }

    public int SalaireAnnuelNet { get; set; }

    public int Rentes { get; set; }

    public int PensionsNettes { get; set; }

    public int BNCBICAnnuellesNetsNMoins1 { get; set; }

    public int BNCBICAnnuellesNetsNMoins2 { get; set; }

    public bool CentreGestionAgree { get; set; }

    public int BeneficeDeficitFoncier { get; set; }

    public int DividendesNMoins1 { get; set; }
    
    public int DividendesNMoins2 { get; set; }

}

I have 2 objects of type EtatCivil

protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        
        modelBuilder.Entity<Annonce>().ToTable("Annonces");
        modelBuilder.Entity<HonorairesAChargeDe>().ToTable("HonorairesAChargeDe");
        modelBuilder.Entity<TypeChauffage>().ToTable("TypesChauffage");
        modelBuilder.Entity<TypeCuisine>().ToTable("TypesCuisine");
        modelBuilder.Entity<Favori>().ToTable("Favoris");
        modelBuilder.Entity<Agenda>().ToTable("Agendas");
        modelBuilder.Entity<Evenement>().ToTable("Evenements");
        modelBuilder.Entity<Offre>().ToTable("Offres");
        modelBuilder.Entity<BilanPatrimonial>().ToTable("BilansPatrimoniaux");
        modelBuilder.Entity<EtatCivil>().ToTable("EtatCivils");
        //modelBuilder.Entity<ChargesImmobilier>().ToTable("ChargesImmobiliers");
        //modelBuilder.Entity<AutresChargesMensuelles>().ToTable("AutresChargesMensuelless");
        //modelBuilder.Entity<Placement>().ToTable("Placements");
    }

When I try to create the database, the FOREIGN KEY 'FK_BilansPatrimoniaux_EtatCivils_EtatCivilMonsieurID1' cannot be created because of cascading cycles or access. The message tells me to specify ON DELETE NO ACTION or ON UPDATE NO ACTION but I don't know how to do this.

Any help would be really appreciated, thanks

  • 1
    Does this answer your question? [Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. - How?](https://stackoverflow.com/questions/63306882/specify-on-delete-no-action-or-on-update-no-action-or-modify-other-foreign-key) – Max Jun 24 '22 at 10:55
  • Yes, it gaves me the right answer, thanks a lot – SylvainKrier Jun 29 '22 at 07:07

0 Answers0