C
C#•7mo ago
Gipper

ASP.NET MVC struggling with putting foreign keys into Db from Model

I have the following class in my Model:
public class SolicitacaoAdesao
{
[Key]
public int Solicitacao_ID { get; set; }

[ForeignKey("idUtilizador")]
//ERROR:
//Introducing FOREIGN KEY constraint 'FK_SolicitacoesAdesao_Utilizadores_UtilizadoridUtilizador' on table 'SolicitacoesAdesao'
//may cause cycles or multiple cascade paths.Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY
//constraints.
public int Utilizador_ID { get; set; }
[ForeignKey("Grupo_ID")]
public int Grupo_ID { get; set; }

public string Status { get; set; }

// Chaves estrangeiras
public virtual Utilizador Utilizador { get; set; }
public virtual Grupo Grupo { get; set; }
}
public class SolicitacaoAdesao
{
[Key]
public int Solicitacao_ID { get; set; }

[ForeignKey("idUtilizador")]
//ERROR:
//Introducing FOREIGN KEY constraint 'FK_SolicitacoesAdesao_Utilizadores_UtilizadoridUtilizador' on table 'SolicitacoesAdesao'
//may cause cycles or multiple cascade paths.Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY
//constraints.
public int Utilizador_ID { get; set; }
[ForeignKey("Grupo_ID")]
public int Grupo_ID { get; set; }

public string Status { get; set; }

// Chaves estrangeiras
public virtual Utilizador Utilizador { get; set; }
public virtual Grupo Grupo { get; set; }
}
Error message is in the comment above. It comes up when I input the update-database command into Package Manager Console. The problem may be in the migration I'm trying to apply into the Db. I'll post the migration code below.
16 Replies
Gipper
Gipper•7mo ago
migrationBuilder.CreateTable(
name: "SolicitacoesAdesao",
columns: table => new
{
Solicitacao_ID = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Utilizador_ID = table.Column<int>(type: "int", nullable: false),
Grupo_ID = table.Column<int>(type: "int", nullable: false),
Status = table.Column<string>(type: "nvarchar(max)", nullable: false),
UtilizadoridUtilizador = table.Column<int>(type: "int", nullable: false),
Grupo_ID1 = table.Column<int>(type: "int", nullable: false)
},
migrationBuilder.CreateTable(
name: "SolicitacoesAdesao",
columns: table => new
{
Solicitacao_ID = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Utilizador_ID = table.Column<int>(type: "int", nullable: false),
Grupo_ID = table.Column<int>(type: "int", nullable: false),
Status = table.Column<string>(type: "nvarchar(max)", nullable: false),
UtilizadoridUtilizador = table.Column<int>(type: "int", nullable: false),
Grupo_ID1 = table.Column<int>(type: "int", nullable: false)
},
Discord won't let me post the whole Migration file cause it's too big. If you feel the need for more, just ask 🙂 Either way thank you for reading my post 🙂
Jorge Morales
Jorge Morales•7mo ago
Hi, I would say it has something to do with how the other end of the foreign key relationship is implemented in Utilizador (or Utilizadores). If you want to implement a parent child relationship between the two, I think the child is SolicitacaoAdesao and Utilizador is the parent, right? If you have the code in GitHub or in any public repo, it would good to see and reproduce the error better It happens to me that I need more context when dealing with EF Core code
Angius
Angius•7mo ago
Rely on convention, not configuration.
public class SolicitacaoAdesao
{
public int ID { get; set; }

public int UtilizadorId { get; set; }
public Utilizador Utilizador { get; set; }

public int GrupoId { get; set; }
public Grupo Grupo { get; set; }

public string Status { get; set; }
}
public class SolicitacaoAdesao
{
public int ID { get; set; }

public int UtilizadorId { get; set; }
public Utilizador Utilizador { get; set; }

public int GrupoId { get; set; }
public Grupo Grupo { get; set; }

public string Status { get; set; }
}
FooId automatically becomes the foreign key to Foo And even if you did want to use attributes, read up on how to actually use [ForeignKey] Spoiler: it's not
[ForeignKey("GrupoId")]
public int GrupoId { get; set; }
public Grupo Grupo { get; set; }
[ForeignKey("GrupoId")]
public int GrupoId { get; set; }
public Grupo Grupo { get; set; }
since it makes no sense. It's
[ForeignKey("Grupo")]
public int GrupoId { get; set; }
public Grupo Grupo { get; set; }
[ForeignKey("Grupo")]
public int GrupoId { get; set; }
public Grupo Grupo { get; set; }
or better yet,
[ForeignKey(nameof(Grupo))]
public int GrupoId { get; set; }
public Grupo Grupo { get; set; }
[ForeignKey(nameof(Grupo))]
public int GrupoId { get; set; }
public Grupo Grupo { get; set; }
Gipper
Gipper•7mo ago
Oh so it's
[ForeignKey("name_of_class")]
[ForeignKey("name_of_class")]
And not the name of the Id variable?
Angius
Angius•7mo ago
Name of the navigation property
Gipper
Gipper•7mo ago
Is that what's causing the error?
Angius
Angius•7mo ago
[ForeignKey(nameof(UngaBunga))]
public int GrupoId { get; set; }
public Grupo UngaBunga { get; set; }
[ForeignKey(nameof(UngaBunga))]
public int GrupoId { get; set; }
public Grupo UngaBunga { get; set; }
And yes, that's most probably what causes the error
Gipper
Gipper•7mo ago
And it has to be specifically nameOfClassId? Can't be nameOfClassID or nameOfClassid, for instance? @ZZZZZZZZZZZZZZZZZZZZZZZZZ
Angius
Angius•7mo ago
If you follow the convention, it should be named NavigationPropertyId for the appropriate NavigationProperty If you want to do manual config, it can be named whatever
Gipper
Gipper•7mo ago
I did what you said and I still get the same error...
Angius
Angius•7mo ago
Show me what you did, and show the error
Gipper
Gipper•7mo ago
The other error message says this if that helps: Failed executing DbCommand (20ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] CREATE TABLE [SolicitacoesAdesao] ( [Solicitacao_ID] int NOT NULL IDENTITY, [Status] nvarchar(max) NULL, [UtilizadorId] int NOT NULL, [GrupoId] int NOT NULL, CONSTRAINT [PK_SolicitacoesAdesao] PRIMARY KEY ([Solicitacao_ID]), CONSTRAINT [FK_SolicitacoesAdesao_Grupos_GrupoId] FOREIGN KEY ([GrupoId]) REFERENCES [Grupos] ([Grupo_ID]) ON DELETE CASCADE, CONSTRAINT [FK_SolicitacoesAdesao_Utilizadores_UtilizadorId] FOREIGN KEY ([UtilizadorId]) REFERENCES [Utilizadores] ([idUtilizador]) ON DELETE CASCADE );
Angius
Angius•7mo ago
Huh... What do Utilizador and Grupo look like?
Gipper
Gipper•7mo ago
I changed the class in question to:
public class SolicitacaoAdesao
{
[Key]
public int SolicitacaoId { get; set; }




public string? Status { get; set; }

// Chaves estrangeiras
public int UtilizadorId { get; set; }
public virtual Utilizador Utilizador { get; set; }
public int GrupoId { get; set; }
public virtual Grupo Grupo { get; set; }
}
public class SolicitacaoAdesao
{
[Key]
public int SolicitacaoId { get; set; }




public string? Status { get; set; }

// Chaves estrangeiras
public int UtilizadorId { get; set; }
public virtual Utilizador Utilizador { get; set; }
public int GrupoId { get; set; }
public virtual Grupo Grupo { get; set; }
}
and I then removed migration and readded migration (several times) and then tried to update-database, upon which I got the same error message: Introducing FOREIGN KEY constraint 'FK_SolicitacoesAdesao_Utilizadores_UtilizadorId' on table 'SolicitacoesAdesao' 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. Utilizador class:
using System.ComponentModel.DataAnnotations;

namespace Trabalho_Lab_Aplicações_Web.Models
{
public class Utilizador
{
[Key]
public int idUtilizador { get; set; }
public string? nome { get; set; }
public string? Email { get; set; }
public string? UserName { get; set; }
public string? Senha { get; set; }
public bool Tipo_Utilizador { get; set; }
public bool Is_Verified { get; set; }
public DateTime? criacao { get; set; }
public bool status { get; set; }
public DateTime? Ultima_Atualizacao { get; set; }
}
}
using System.ComponentModel.DataAnnotations;

namespace Trabalho_Lab_Aplicações_Web.Models
{
public class Utilizador
{
[Key]
public int idUtilizador { get; set; }
public string? nome { get; set; }
public string? Email { get; set; }
public string? UserName { get; set; }
public string? Senha { get; set; }
public bool Tipo_Utilizador { get; set; }
public bool Is_Verified { get; set; }
public DateTime? criacao { get; set; }
public bool status { get; set; }
public DateTime? Ultima_Atualizacao { get; set; }
}
}
Grupo class:
public class Grupo
{
[Key]
public int Grupo_ID { get; set; }

public string Nome_grupo { get; set; }

public string Descricao { get; set; }

public string Categoria { get; set; }

public bool Privacidade { get; set; }

[ForeignKey("Criador_ID")]
public int Criador_ID { get; set; }

public DateTime Criado_em { get; set; }

// Chave estrangeira
public virtual Utilizador Criador { get; set; }
}
public class Grupo
{
[Key]
public int Grupo_ID { get; set; }

public string Nome_grupo { get; set; }

public string Descricao { get; set; }

public string Categoria { get; set; }

public bool Privacidade { get; set; }

[ForeignKey("Criador_ID")]
public int Criador_ID { get; set; }

public DateTime Criado_em { get; set; }

// Chave estrangeira
public virtual Utilizador Criador { get; set; }
}
Angius
Angius•7mo ago
I'd maybe look into this part of the error:
Specify ON DELETE NO ACTION or ON UPDATE NO ACTION
Specify ON DELETE NO ACTION or ON UPDATE NO ACTION
Idk how to specify delete behaviour with attributes, so you'll have to look into that
Gipper
Gipper•7mo ago
Yeah I had several other model classes where I didn't follow the convention you pointed out above, once I changed to follow the convention it created Db fine, thanks 🙂 @ZZZZZZZZZZZZZZZZZZZZZZZZZ