Reputation:
I have recently changed from Windows to MacOS and I'd like to make a simple project with ASP.NET Core Entity Framework with CRUD operations. I want to use Database First.
So please can someone guide me in the right path, how I can setup an ASP.NET Core Entity Framework with Database First. I been stuck for a few weeks now.
I use Visual Studio and Workbench.
Upvotes: 1
Views: 3481
Reputation: 238
You should be able to do so via the dotnet cli, and reverse engineer your database design into EF Core entities. So eg. open terminal and type in the
dotnet ef dbcontext scaffold "Server={sql_server_name};Database={database_name};Trusted Connection=True;" Microsoft.EntityFramework.SqlServer -o Models
breaking the cli command up it would be
dotnet ef
is the ef core part of the cli
dbcontext scaffold
is the name of the command
"Server={sql_server_name};Database={database_name};Trusted Connection=True;"
would be your connection string
so depending on what sql server you are, I would assume it's a mysql with workbench which means you would probably type in a username and password, so instead of Trusted Connection=True; you would provide a uid={username} and pwd={password}
Microsoft.EntityFramework.SqlServer
this tells the cli which databaseprovider to use.
-o Models
is the name of the output folder you want the generated models to be generated in.
with all these things in mind I would suggest you'd try this in the root folder of your project.
dotnet ef dbcontext scaffold "server={sql_server_name};database={database_name};uid={db-username};pwd={db-password};" Microsoft.EntityFramework.SqlServer -o Models
Link to the documentation:
https://www.entityframeworktutorial.net/efcore/create-model-for-existing-database-in-ef-core.aspx
Upvotes: 1