Closed ail-milan closed 2 years ago
This sounds like an EF Core problem. JADNC just consumes the model to serve API requests, it has nothing to do with migrations. You'll probably get the same error when not using JADNC at all. Can you verify that?
If I remove the line
builder.Services.AddJsonApi<DatabaseContext>();
it will work perfectly ...
I'm quite surprised to hear that. I'm willing to help and find the root cause. For that I'm going to need to reproduce this, but my knowledge of how migrations work is superficial. It would help if you can provide a minimal repro project (zip or GitHub repro) with the exact steps to get to the error (for example, from which directory you're running the command). It would also help to include the versions used (OS, .NET, EF, etc)
Wouldn't you normally have to specify the project that contains the DbContext if is it not in the main (startup) one? EF Core migrations will not find it otherwise.
I've tried to set things up based on your description, but am unable to reproduce the problem.
You can find the project here: MigrationsDemo.zip. It contains a git repository, so you can review my changes or revert individual steps.
To start the PostgreSQL database in a docker container, run this script. When running the application, it returns the expected error that the database does not yet exist.
Then, I'm running the below commands:
PS D:\Bart\Source\Projects\MigrationsDemo\WebApi> dotnet tool restore
Tool 'dotnet-ef' (version '6.0.9') was restored. Available commands: dotnet-ef
Restore was successful.
PS D:\Bart\Source\Projects\MigrationsDemo\WebApi> dotnet dotnet-ef migrations add InitDB
Build started...
Build succeeded.
Done. To undo this action, use 'ef migrations remove'
PS D:\Bart\Source\Projects\MigrationsDemo\WebApi> dotnet dotnet-ef database update
Build started...
Build succeeded.
Applying migration '20221007210202_InitDB'.
Done.
And now the application starts normally (the table is empty, obviously):
{
"links": {
"self": "https://localhost:7233/people",
"first": "https://localhost:7233/people"
},
"data": []
}
I have just tried with your given sample code, and it will work perfectly!! Here I have created sample from your code, and it is working fine, DemoAPI Project
Closing this issue!! Thanks for guiding me.
Glad to hear this works for you. And thanks for sharing your project.
DESCRIPTION
I have created a solution. which have 2 projects,
Project A
(WebAPI) - .NET 6.0Project B
(Class Library c#) - .NET 6.0Now what happed is my DbContext is in the
Project B
, all my DB related stuff is in theProject B
All my controllers are in theProject A
.Project A
has a reference toProject B
.Project A
Program.cs
content ...Project B
DatabaseContext.cs
content ...Now try to create Migrations using below command...
it will be giving me error ...