I’m facing a problem creating the initial migration for my ASP.NET project. After defining models and data annotations, when trying to create the database, I encounter the following error in the NuGet Console:
Failed executing DbCommand (7ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [FCT] (
[IdFCT] int NOT NULL IDENTITY,
[AlunoIdAluno] int NOT NULL,
[EmpresaIdEmpresa] int NOT NULL,
[InstituicaoEnsinoIdInstituicao] int NOT NULL,
[DataInicioFCT] datetime2 NOT NULL,
[DataFimFCT] datetime2 NOT NULL,
[PeriodoFCTIdPeriodo] int NOT NULL,
[AvaliacaoIdAvaliacao] int NOT NULL,
[DocumentosIdDocumento] int NOT NULL,
CONSTRAINT [PK_FCT] PRIMARY KEY ([IdFCT]),
CONSTRAINT [FK_FCT_Alunos_AlunoIdAluno] FOREIGN KEY ([AlunoIdAluno]) REFERENCES [Alunos] ([IdAluno]) ON DELETE CASCADE,
CONSTRAINT [FK_FCT_Avaliacao_AvaliacaoIdAvaliacao] FOREIGN KEY ([AvaliacaoIdAvaliacao]) REFERENCES [Avaliacao] ([IdAvaliacao]) ON DELETE CASCADE,
CONSTRAINT [FK_FCT_Documentos_DocumentosIdDocumento] FOREIGN KEY ([DocumentosIdDocumento]) REFERENCES [Documentos] ([IdDocumento]) ON DELETE CASCADE,
CONSTRAINT [FK_FCT_Empresa_EmpresaIdEmpresa] FOREIGN KEY ([EmpresaIdEmpresa]) REFERENCES [Empresa] ([IdEmpresa]) ON DELETE CASCADE,
CONSTRAINT [FK_FCT_InstituicaoEnsino_InstituicaoEnsinoIdInstituicao] FOREIGN KEY ([InstituicaoEnsinoIdInstituicao]) REFERENCES [InstituicaoEnsino] ([IdInstituicao]) ON DELETE CASCADE,
CONSTRAINT [FK_FCT_PeriodoFCT_PeriodoFCTIdPeriodo] FOREIGN KEY ([PeriodoFCTIdPeriodo]) REFERENCES [PeriodoFCT] ([IdPeriodo]) ON DELETE CASCADE
Introducing FOREIGN KEY constraint 'FK_FCT_InstituicaoEnsino_InstituicaoEnsinoIdInstituicao' on table 'FCT' 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.
Here are my Models (the ones that I think that are causing the problem):
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace FCTConnect.Models
{
public class FCT
{
[Key]
public int IdFCT { get; set; }
public Aluno Aluno { get; set; }
public Empresa Empresa { get; set; }
public InstituicaoEnsino InstituicaoEnsino { get; set; }
[Required]
[DataType(DataType.Date)]
public DateTime DataInicioFCT { get; set; }
[Required]
[DataType(DataType.Date)]
public DateTime DataFimFCT { get; set; }
public PeriodoFCT PeriodoFCT { get; set; }
public Avaliacao Avaliacao { get; set; }
public Documentos Documentos { get; set; }
}
}
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace FCTConnect.Models
{
public class Aluno
{
[Key]
public int IdAluno { get; set; }
[Required]
[StringLength(100)]
public string Nome { get; set; }
[Required]
public int Idade { get; set; }
[Required]
[StringLength(100)]
public string Curso { get; set; }
[Required]
[EmailAddress]
public string Email { get; set; }
[Required]
[Phone]
public string Telefone { get; set; }
[Required]
[StringLength(100)]
public string Morada { get; set; }
public InstituicaoEnsino InstituicaoEnsino { get; set; }
}
}
using System.ComponentModel.DataAnnotations;
namespace FCTConnect.Models
{
public class InstituicaoEnsino
{
[Key]
public int IdInstituicao { get; set; }
[Required]
[StringLength(100)]
public string Nome { get; set; }
[Required]
[StringLength(100)]
public string Morada { get; set; }
[Required]
[EmailAddress]
public string Email { get; set; }
[Required]
[Phone]
public string Telefone { get; set; }
[Required]
[StringLength(500)]
public string Descricao { get; set; }
}
}
In the previous phase, I develop the following E-R Diagram:
e-r diagram
My project is being developed in Visual Studio with the SQL Server.
I’m grateful to anyone who can help.
I tried to specify ON DELETE NO ACTION or ON UPDATE NO ACTION but I was unsuccessful.
I tried defining foreign keys instead of declaring classes directly, but I think that declaring classes directly will be more advantageous in program development.