How to share DbContexts and not get 2 user tables - entity-framework

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.

Related

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

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

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;"

EF6 + Postgres relation dbo.AspNetUsers does not exist

I have been following this post on using PostgreSQL with EF6 http://www.jasoncavett.com/blog/postgresql-and-entity-framework-6-code-first/.
I have started a brand new MVC5 project hoping to use Postgres in my application for backend. The application starts up fine however when you go to register a user (I selected individual authentication) I get the following error messsage
ERROR: 42P01: relation "public.AspNetUsers" does not exist
I am unsure as to how to resolve this problem.
The error happens on line 155 which can be seen here
More information can be provided if needed.
I had ran the application before migrating to Postgres so all I needed to was to add a migration and update database through the package manager console.
If you see this Error in 2018, the answer is because the table has not being added to the migrations.
dotnet ef migrations add 01_users &&
dotnet ef database update
It is interesting but when i write as below it works on tableplus;
select *
from "AspNetUsers";
or
select "UserName"
from "AspNetUsers";

moving to a new database with Code First Migrations

I have recently started using code first and migrations and I'm pretty happy with it.. I have been following the constant pattern of add-migration and update-database!
I have just tried to move from localdb to SQL Express and im having a real pain..
when I try and run the application.. I get the follow error..
Cannot find the object "dbo.AspNetUsers" because it does not exist or you do not have permissions.
In my Global file I have..
Database.SetInitializer(new MigrateDatabaseToLatestVersion());
Any ideas? It looks like the core forms tables are not being created?
if I run my application without the Initializer in the global file I get this.
Migrations is enabled for context 'ApplicationDbContext' but the database does not exist or contains no mapped tables. Use Migrations to create the database and its tables, for example by running the 'Update-Database' command from the Package Manager Console.
Thanks
Ste.

ActiveRecord from an existing PostgreSQL database

I've been looking all around the web and i couldnt found a concrete solution to my issue, i want to create a model Client that has an existing table clients in the PostgreSQL server.
Im using rails 4, and i've been trying to create the model Client and the controller Clients like this:
class ClientsController < ApplicationController
scaffold:client
end
But when calling localhost:3000/clients/new a warning of pending migrations appear. It has no sense making the migration if the database and table are already there with information, how can i do to notice rails that the migrations have to be ignored?
UPDATE:
I have generated a model and a controller in rails 4,Client and ClientsController, and configure the database.yml with the postgres adapater and the database name, and i want that rails get the table attributes from the clients table and generate the forms and all the scaffolding CRUD operations. I've seen in the web some similar solutions but with mysql database and i think with rails 3...
Any help is welcome
Did you run a generator to create the model and controller? If so, did it also create a migration file for you under db/migrate?
Delete the migration file and Rails should stop complaining about pending migrations.
If you have other migrations that are pending, run rake db:migrate to run them.