Scaffold-DbContext creating model for table without a primary key - entity-framework

I am trying to create DBcontext and corresponding model for a particular table in ASP.NET core MVC application. This table doesn't have any primary key.
I am running following Scaffold-DbContext command-
Scaffold-DbContext "Server=XXXXX;Database=XXXXXXX;User Id=XXXXXXX;password=XXXXXXX" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -t TABLE_NAME -force -verbose
In Package Manager Console, I can see this verbose output-
...............
...............
Unable to identify the primary key for table 'dbo.TABLE_NAME'.
Unable to generate entity type for table 'dbo.TABLE_NAME'.
My Environment is - VS2015 update3, .NET core 1.0.0, ASP.NET MVC core application.
Is there anyway to create a model for a table without primary key?

Support for complex types (those that don't have an entity key) in EF Core is on the backlog: https://github.com/aspnet/EntityFramework/issues/246. That's why any attempt to reverse engineer a table without a primary key defined won't work at the moment.

Related

if we update our database how we add model class in our project in database first approach EF core

When I add a new table in my database which use in my database first approach EF project I get some error and problem in my project and newly created DB wasn't attached in my project.
Step 1: After Create Table in Database then open you Package Manager Console
Step 2: Use the following Command for add your new table model class
Scaffold-DbContext "Server=Server name;Database=Database name;User
Id=User name;Password=password;"
Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Force
Step 3: Hit Enter
Note: Here only add -Force command with previous migration command

(DB first)I have added one more tables in database after Scaffold. Now trying to add those tables into my model in Entity Framework Core

I have tried it, but it create a master context do not add any new tables into model.
Scaffold-DbContext "Server=localhost;Database=master;Trusted_Connection=True;"
Microsoft.EntityFrameworkCore.SqlServer
-OutputDir Models -Tables OrderDetails -f
Please help.
In the scaffold command you show, you're scaffolding the Master database. That's probably not your application database, or at least it shouldn't be. Master is a system database, and your application should have its own database.
You could add the new tables manually as EF core model classes.

How to generate Entity-context only for one schema when multiple schema available in one database in Postgres sql with .Net Core?

I am using the Postgres database for my project and have created 3 schemas named mydb_dev, mydb_stage, and mydb_prod in database 'temp_db'.
I am creating .Net core web application with the database first approach with entity framework.
Now, whenever I am trying to generate an entity model from the database it generates for all 3 databases. And I want to generate only one database that is mydb_dev.
I am using the following command to generate through the console.
Scaffold-DbContext "Host=localhost;Database=temp_db;Username=myuser;Password=mypassword" Npgsql.EntityFrameworkCore.PostgreSQL -o Models
Is there any way to generate an entity model only for one schema?
If you check the documentation: https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/powershell#scaffold-dbcontext
Scaffold-DbContext has a -Schemas <String[]> parameter.
The schemas of tables to generate entity types for. If this parameter
is omitted, all schemas are included.
So your command should look like this:
Scaffold-DbContext "Host=localhost;Database=temp_db;Username=myuser;Password=mypassword" Npgsql.EntityFrameworkCore.PostgreSQL -o Models -Schemas "mydb_dev"

Update database from models using entity framework in multi tenant .net core

I am using .net core entity framework.
I have multi tenant dbs. so I have kept one root tenant db as base. and I want to replicate those schema changes to all other dbs using entity framework. I am generating my models using following command.
Scaffold-DbContext "Data Source=(local);Initial Catalog=sampleTenantDb;Integrated Security=True" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Tenants -Force
So when creating new tenant, I simply use
context.Database.EnsureCreated();
But when I add new table in code, I want to apply it to all tenants. So how do I do it?
I tried following but, it doesn't work (not adding remanining tables)
myDbContext.Database.Migrate();
If your DbContext is created with Scaffold-DbContext, it won't have any Migrations. That's for a database-first workflow, in which you would apply the DDL changes to the tenant databases with a script, perhaps created with SQL Server Data Tools.
You have need to create multiple dbcontext object and and pass connection string in that way you can update multiple db

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.