We continue the theme of the Entity Framework commands! Commands that we have already seen:
Scaffold-DbContext
This is the command that you should use when you have an existing database and you want to generate from it a data context and all the respective classes that represent the tables of said database. We call this procedure database first. We have an entry which talks more about this technique. In this entry we will study the Scaffold-DbContext command.
In the case of the dotnet CLI, you can find this command in:
dotnet ef dbcontext scaffold
For this command, we can highlight the following parameters:
- Connection: This is the connection string to the database.
- Provider: The provider to use. Remember that the provider is the library that makes EF Core work with a specific database engine.
- OutputDir: Location where the generated files will be placed.
- Force: Overwrite files if necessary. This means that when they are going to generate the corresponding files, if a file is in danger of being overwritten, EF Core halts the whole operation, however, if we indicate the -force option, then the file will be overwritten.
- Schemas: The names of the schemas to which the tables that we want to generate as classes belong
- Tables: The tables that we want to generate.
- DataAnnotations: If we want data annotations to be generated when possible, otherwise, all configurations will be done using the Fluent API.
There are other parameters which you can see using the Get-Help command.
Examples
In the following examples we will work under the assumption that we will use the SQL Server provider, which we will represent as [provider], and that our connection string is the following [connectionString], this to shorten space in the examples. Here is an example of a connection String:
Data Source=(localdb)\mssqllocaldb;Initial Catalog=MyDatabase;Integrated Security=True”
And, the provider that I will be using: Microsoft.EntityFrameworkCore.SqlServer
Basic use: The basic use would be that we want to generate all the tables of a database as classes in our application. For that. In the Package Manager Console, we can execute the following command:
Scaffold-DbContext [ConnectionString] [provider]
In the dotnet CLI:
dotnet ef dbcontext scaffold [ConnectionString] [provider]
Only two specific tables: Suppose that from our large database, we only want two specific tables, called Students and Contacts, to generate only those two tables, in the Package Manager Console, we can execute the following command:
Scaffold-DbContext [ConnectionString] [provider] -Tables Students,Contacts
In the dotnet CLI:
dotnet ef dbcontext scaffold [ConnectionString] [provider] --table Students --table Contacts
Hola, buen día
Estoy desarrollando una web api en .NetCore con Entity Core; las clases las genero a partir de la base de datos mediante Scaffold-DbContext
Luego de ejecutar SaveChangeAsync, me da un error: The target table ‘CTACTESATRIB’ of the MERGE statement cannot have any enabled rules. Found rule ‘CUEPREFI’.
Veo que mi base de datos tiene definida una regla de validación en (Programabbility / Rules) con lo cual estimo que el error se debe a que mi proyecto desconoce estas reglas.
CONSULTA: Como debería agregar a mi proyecto las reglas de validación usando el Scaffold ??
saludos y gracias
Daniel
d_salum@hotmail.com
LikeLike
Buenas tardes,
Lo primero que he encontrado acerca de las reglas es que, quizás, puedas cambiarlas por Check Constraints. ¿Entiendes que esto te aplica? Si la respuesta es no, déjame saber para buscar otra solución.
Link: https://stackoverflow.com/questions/6504173/why-is-the-target-table-of-a-merge-statement-not-allowed-to-have-enabled-rules
Saludos
LikeLike