Generate POCO classes in Asp.net Core:
Step1: Install NuGet packages
The following NuGet packages should be added to work with the SQL Server database and scaffolding. Run these commands in Package Manager Console:
Install-Package Microsoft.VisualStudio.Web.CodeGeneration.Design
Install-Package Microsoft.EntityFrameworkCore.Tools
Install-Package Microsoft.EntityFrameworkCore.SqlServer
Step2:
Scaffolding:
ASP.NET Core has a feature called scaffolding, which uses T4 templates to generate code
Run the following scaffold command in Package Manager Console:
Scaffold-DbContext "Server=***sqll.database.windows.net;Initial Catalog=TestDB;Persist Security Info=False;User ID=sa;Password=password1;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" Microsoft.EntityFrameworkCore.SqlServer -force -Schemas "dbo","otherdbo" -OutputDir EFData -Context EFDataContext -DataAnnotations
or (with out schema)
Scaffold-DbContext "Server=***-sql.database.windows.net;Initial Catalog=TestDB;Persist Security Info=False;User ID=sa;Password=password1;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir EFData -Context EFDataContext
Use -force flog to override existing generated classes:
Scaffold-DbContext "Server=***dev-sql.database.windows.net;Initial Catalog=HealthGrid;Persist Security Info=False;User ID=sa;Password=password1;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" Microsoft.EntityFrameworkCore.SqlServer -force -OutputDir EFData -Context ClinicalTrialDataContext
Simply:
Scaffold-DbContext “DataContext “Server=***sql.database.windows.net;Database=TestDb;Integrated Security=True” Microsoft.EntityFrameworkCore.SqlServer -OutputDir EFData"
or
Scaffold-DbContext "Data Source=***-sql.database.windows.net;Initial Catalog=TestDd;Persist Security Info=True;User ID=sa;Password=password1;MultipleActiveResultSets=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir EFData -Context DataContext
Generate for specific table:
Scaffold-DbContext "server=localhost;user=sa;password=pass;database=testdb"
MySql.Data.EntityFrameworkCore -OutputDir EFData
-Tables actor,film,film_actor,language -f
No comments:
Post a Comment