You may get into a situation where you have an existing SQL Server, and you want to build a code first DB approach using Entity Framework core.
You can use Scaffold-DbContext
to create a model based on the existing database.
Below are the parameters that can be specified with Scaffold-DbContext in Package Manager Console:
Scaffold-DbContext [-Connection] [-Provider] [-OutputDir] [-Context] [-Schemas>] [-Tables>]
[-DataAnnotations] [-Force] [-Project] [-StartupProject] [<CommonParameters>]
In Visual Studio, select menu Tools --> NuGet Package Manger --> Package Manger Console and run the following command:
Scaffold-DbContext "Server=.\SQLExpress;Database=SchoolDB;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
If you get an error like :
A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - The certificate chain was issued by an authority that is not trusted.
add attribute : TrustServerCertificate=True to the command. So your final command will look like:
Scaffold-DbContext "Server=servername\instaneName;Database=dbName;user id=userID;password=passwordValue;TrustServerCertificate=True" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
No comments:
Post a Comment