Right now, the only way (as far as I know) to scaffold a database is via the command: Scaffold-DbContext .....
This does not scaffold tables without primary keys or views and does not run if there are any errors in your code. Also, I believe in order to update one table, you have to scaffold the entire database again (correct me if I'm wrong)
Will we see something like .edmx files in past ASP.NET versions? Something with a GUI or just less error-prone?
Is there another way to do it that I've missed?
We can Scaffold the entire database tables with following Package Manager Console Command
Scaffold-DbContext "Server=yourserver;Database=database;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir testDir 'directory to save the scaffolded tables'
To Scaffold one table
Scaffold-DbContext "Server=yourserver;Database=database;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir testDir -t table-name
To Update the existing table
Scaffold-DbContext "Server=yourserver;Database=database;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir testDir -t table-name -force -verbose
Related
I can't import with Pomelo on C#, a several tables without data.
Scaffold-DbContext "'Database'" Pomelo.EntityFrameworkCore.MySql" -Table 'table1' 'table2
I have an error :
sequence contains no matching element
Do you have any solution?
I have resolve the problem but i'm not sur it's good practice.
I import the all table with :
Scaffold-DbContext "'Database'" Pomelo.EntityFrameworkCore.MySql"
and I delete the files of others tables that I don't use.
Scaffolding a database and stating individual tables works fine with Pomelo 3.2.2:
CLI:
dotnet ef dbcontext scaffold "server=127.0.0.1;uid=root;pwd=;port=3306;database=So64222039" Pomelo.EntityFrameworkCore.MySql -c MyContext --verbose -t IceCreams -t IceCreamShops
Package Manager:
Scaffold-DbContext "server=127.0.0.1;uid=root;pwd=;port=3306;database=So64222039" Pomelo.EntityFrameworkCore.MySql -Context MyContext -Verbose -Tables IceCreams,IceCreamShops
The syntax to specify multiple tables is different when using the VS Package Manager instead of the CLI (use -Tables table1,table2,table3 instead of -t table1 -t table2 -t table3).
Starting a new project, and trying to set up the database for authentication. I run the following command in powershell.
dotnet ef database update -Context ApplicationDbContext
and get the following error Unrecognized option '-Context'
So I try running it without the -Context option just to see and get:
More than one DbContext was found. Specify which one to use. Use the '-Context' parameter
for PowerShell commands and the '--context' parameter for dotnet commands.
I also tried running it with --context instead of -context, but get the same error. Any suggestions on why it recognizes that I need that option, but at the same time tells me it doesn't recognize the option?
I also restart powershell.
Instead of using --context ApplicationDbContext have you tried -c ApplicationDbContext
https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet#aspnet-core-environment
I was having the same problem and instead of using dotnet ef database update -Context ApplicationDbContext, I replaced -Context with -c which worked for me.
I'm working on a project with different implementations of DbContext. Using EF code first, I generate migration :
Enable-Migrations -ContextTypeName <> -Force
Add-Migration <>_DB_v1.0
Update-Database -Verbose
The update always tries to attach the *.mdf file but no file is ever genrated in local, and I'm working on a distant SQL Server (Distant virtual DB)
the Update-Script option successfully generates the SQL file and well executed on the Db, but I always get error to execute last update before going on new migration.
Could any body help ?
Project is in .net 4.6, and using EF 6.0.
Sorry not able to paste code for confidentiality. I may ensure that All connection string, project conf. and so are perfectly set.
Please ensure that migration history table in the Db and Migrations files in your Solution are match.
I think you miss some migration.
I found the error reason:
1- Indicate explicitly the providerName="System.Data.SqlClient" in the connectionStrings definition
2- Executing the migration, specify the -ConnectionStringName, even if the name displaed in the console seems to be OK.
Add-Migration DB_vX.x -ConnectionStringName XXX_Database
update-database -Verbose -Script -ConnectionStringName XXX_Database
I am using an existing Database with a new ASP.Net Core 2.0 application. The database has two schemas, dbo and notinapplication. I do not want to create model of notinapplication schema tables. So I use the following code in Package manager and it works fine.
Scaffold-DbContext "Server=localhost; Database=TestServer; Trusted_Connection=True;
MultipleActiveResultSets=true;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
-UseDatabaseNames -Force -Context "DbContext" -Schema "dbo"
This way I only get tables from dbo in DbContext and the notinapplication schema tables are ignored.
However now I have a new schema called user that needs to be part of the model.
Scaffold-DbContext "Server=localhost; Database=TestServer; Trusted_Connection=True;
MultipleActiveResultSets=true;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
-UseDatabaseNames -Force -Context "DbContext" -Schema "user"
But using the above code eliminates the tables from dbo schema. What are my options to have tables of both schemas in DBContext while ignoring the notinapplication schema.
And if I indeed have to create different contexts, is it possible to query from multiple DB contexts in one query?
All you need to do to provide multiple values is to use the 'array syntax'.
-Schema "schema1","schema2","schema3"
In your case, you have to do
Scaffold-DbContext "Server=localhost; Database=TestServer; Trusted_Connection=True;
MultipleActiveResultSets=true;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
-UseDatabaseNames -Force -Context "DbContext" -Schema "dbo","user"
You can do the job by using -Schema parameter multiple times in this way though :/
-Schema "dbo" -Schema "user"
I'm attempting to setup store for my IdentityServer4 Token server,
I'm following along with this tutorial where I encountered database migrations like so:
dotnet ef migrations add InitialIdentityServerMigration -c PersistedGrantDbContext
dotnet ef migrations add InitialIdentityServerMigration -c ConfigurationDbContext
dotnet ef migrations add InitialIdentityServerMigration -c ApplicationDbContext
Apparently I'm running a different version of powershell, or the tools are messed up so I have to run my migrations using a different syntax
Add-Migration InitialIdentityServerPersistedGrantDbMigration -c PersistedGrantDbContext -o Data/Migrations/IdentityServer/PersistedGrantDb
Add-Migration InitialIdentityServerConfigurationDbMigration -c ConfigurationDbContext -o Data/Migrations/IdentityServer/ConfigurationDb
Theese two commands worked great however I'm still missing the third command to generate the ApplicationDbContext:
When I run I get this exception:
SqlException: Invalid object name 'AspNetUsers'.
I'm missing this table along with a few others from the database for Identity, does anyone know which Migration to use?
If you create the default ASP.NET project with authentication, you will see a migration file 00000000000000_CreateIdentitySchema.cs.
So
dotnet ef migrations add CreateIdentitySchema -c ApplicationDbContext
should work.
It turns out it was simple. I though Migrations were generating the Db from something that it had in code, but in actuality it seems to be constructing the database based off of the CLR Class configuration
Add-Migration InitialStillDontKnowWhatThisNameIsFor -c ApplicationDbContext -o Data/Migrations/IdentityServer/PersistedGrantDb
I don't know what the names is for but that will generate the DB