What is --project in dotnet ef database update for? - entity-framework

I have a solution consisting of 4 projects:
EntityProject : contains POCO classes.
ContextProject : contains database context derived from DbContext, has a reference to EntityProject.
MigrationProject : contains public class Empty{}, has a reference to ContextProject.
StartupProject : for example Asp.Net Core Webapi, has references to ContextProject and MigrationProject.
In StartupProject, I invoke MigrationAssembly(typeof(MigrationProject.Empty).Assembly.GetName().Name) to change the migration assembly project from the default ContextProject to MigrationProject.
If the current working directory is the solution directory, I usually do the following and it works.
migration with
dotnet ef migrations add Initial --project MigrationProject --startup-project StartupProject
where --project is mandatory.
updating database with
dotnet ef database update --startup-project StartupProject
where --project is omitted.
Now I am wondering why dotnet ef database update also has --project switch, what is it for? Is there an example in which --project is mandatory?
Any comments are always welcome!

It is just a general parameter not used by the Update database code path
See source here: https://github.com/dotnet/efcore/blob/main/src/EFCore.Design/Design/Internal/MigrationsOperations.cs#L197

Related

How to share DbContexts and not get 2 user tables

I'm using Asp.Net Core 3.1 with Entity Framework, Asp.Net Identity, and IdentityServer4. I'm trying to share my DbContexts across several projects but my code first migrations aren't producing what I need. Is this possible with code first migrations? Is my setup completely asinine? Thank you for your time.
My project hierarchy with DbContexts.
What I want
What I get
In both Identity and Application Startups
services.AddIdentity<AppUser, IdentityRole>()
.AddEntityFrameworkStores<AppUserContext>()
.AddDefaultTokenProviders();
...
services.AddDbContext<AppUserContext>(options => options.UseSqlServer(connectionString));
Migrations
dotnet-ef migrations add Init -p User\User.csproj -c AppUserContext -s IdentityServer\IdentityServer.csproj
dotnet-ef migrations add Init -p Persistence\Persistence.csproj -c RecipeContext -s Application\Application.csproj
Update 1
Moving AppUser and AppUserContext to IdentityServer. Red is a restricted endpoint.
Thanks to #RuardvanElburg's advice, I "solved" this by changing my paradigm which you can see in my Update 1.

How to update existing model class generated by Scaffold-DbContext

Working with ASP.NET CORE EF, I have generated model classes from existing database with following command:
Scaffold-DbContext "Server=myserver\mydb;Database=mydb;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
Now my database is changed, for example I added a new column in a table AppUser. I don't want to use migrations to keep sync models with database.
I already made changes in database and now I want to update my existing model classes from current database state. Any suggestion how to do this?
Should I delete my existing model classes and regenerate by using the same command Scaffold-DbContext or do we have any other way make existing model classes to reflect new changes.
Are you looking for something like this?
As far as I know, to update the model you must execute the same command to overwrite the changes but with another additional flag.
I remember using the -f (force) option to overwrite the changes:
Scaffold-DbContext "Server=myserver\mydb;Database=mydb;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -f
Although it is also possible to indicate which entity you want to update (table):
Scaffold-DbContext "Server=myserver\mydb;Database=mydb;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -t <table> -f
Scaffold-DbContext can be used with option -Context to expand the current DbContext file, instead of creating a new one.
Example:
Scaffold-DbContext "Server=server;Database=mydb;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -f -Context MyDbContext
The generated code for the MyDbContext will be placed on a partial class file.

Entity Framework Core on Azure: Update-Database parameter –ConnectionString does not work

I want to update database on Azure SQL to fit .Net Core application using Entity Framework Core migrations. The following command is advised online:
Update-Database -ConnectionString $ConnectionString -ConnectionProviderName "System.Data.SqlClient"
However, it does not work since Update-Database cmdlet does not recognize parameter –ConnectionString. How can automate database migrations?
How can automate database migrations?
EF does not support Automatic migrations, you may need to manually execute Add-Migration or dotnet ef migrations add for adding migration files. You could explicitly execute the command to apply the migrations, also you could apply migrations in your code.
If you want to apply migration at runtime, you could add the following code int the Configure method of Startup.cs
using (var scope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
scope.ServiceProvider.GetRequiredService<ApplicationDbContext>().Database.Migrate();
}
For more details, you could refer to this thread.

Entity Framework Core - recreate database from the CLI

During development, I often want to clear out the data in my Entity Framework (SQL) database and rebuild the empty table structure using existing migrations. I'd like to do this with dotnet ef commands.
Dropping the DB is easy:
dotnet ef database drop {OPTIONAL: --project MyProject --context MyContext}
I had thought I could rescaffold with the update command:
dotnet ef database update {OPTIONAL: --project MyProject --context MyContext}
But this fails with a "login failed for user" error. My DB is hosted in Azure, so I'm having to manually recreate the database in the portal, update my connection string and then run the update command.

Entity Framework core won't update database

I successfully created an Entity Framework Core migration and updated the database with it.
Then after I added another class, I created a second migration called "update1" which created a class of the same name from the command line tools.
However, when I attempt to update the database, it fails.
Here is the commands I used
dotnet ef migrations add update1 -c MyDbContext
dotnet ef database update update1 -c MyDbContext
and it failed with
There is already an object named MyTable in the database
which is a table which was created in the initial migration.
How can I tell it to either ignore the error, or else to only attempt to run the update1 migration?
Edit: deleting the table that was already there caused this odd behavior to stop happening and now it works as expected.
Thanks
You have to remove the unwanted migration from the __MigrationHistory table.After that you can run your latest migration.This is happened due to you have manually deleted the table.B'cos EF doesn't know anything about your manual operations hence __MigrationHistory table still exist your old migration details (i.e. manually deleted table's record).
In my case I used Ubuntu, EF 6 and the database was in the docker and it's doesn't updated DB.
I added --connection attribute and it's works, an example:
sudo dotnet ef database update --connection
"Server=localhost;Database=temp;User Id=sa;Password=xxxxxxx;"