ASP.NET MVC struggling with putting foreign keys into Db from Model
I have the following class in my Model:
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
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 🙂
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
Rely on convention, not configuration.
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
since it makes no sense. It's
or better yet,
Oh so it's
And not the name of the Id variable?
Name of the navigation property
Is that what's causing the error?
And yes, that's most probably what causes the error
And it has to be specifically nameOfClassId?
Can't be nameOfClassID or nameOfClassid, for instance?
@ZZZZZZZZZZZZZZZZZZZZZZZZZ
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 whateverI did what you said and I still get the same error...
Show me what you did, and show the error
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
);
Huh... What do
Utilizador
and Grupo
look like?I changed the class in question to:
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:
Grupo class:
I'd maybe look into this part of the error:
Idk how to specify delete behaviour with attributes, so you'll have to look into that
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