Skip to main content

Scaffold Existing SQL Database Using Entity Framework Core

My photo
Introduction.
There are many data access technique and tools out there that allow our application  interact with our data store, things like AutoMapper, Dapper and many more. In this article we are making use of Entity Framework Core to create our database context for communicating with our data store ( in this case SQL RDBMS ).

Assumptions
This article assume that we are going to perform data access for an ASP.Net Core application. All crosscutting concern will be ignored

Consideration
The code involved in this article is based on assumptions, It was proposed for the intention to show its functions/features based on the article title. For production/release we might need to tweak it to meet the company policy, best practice and industry standards. The code/script snippet is free to use anywhere.

The Requirement Statement
We are required to create a data access library that will be consumed by a client ( Asp.Net Core ) . This library then will be referenced by the client to perform database CRUDs .

Things Needed To Accomplish
1. Visual Studio 2017/2019
2. Existing Database ( required credential if database is hosted elsewhere )

1.Create A Library Project
We start building our data access component by creating a dll library project. (.Net Core).


2.Open The Package Management Console. 



3.Install The Required Packages
Since we are targeting ASP.Net Core App. We will make use of Microsoft.AspNetCore.App package. Install the package by typing/pasting below script

Install-Package Microsoft.AspNetCore.App 

4.Scaffold The Database To Model And Context
Next, we will require the database connection string to perform the scaffolding. We also required to name the folder where all the models and database context will be saved. Once we have all the information. we may then run the script to perform the scaffolding.

The script is simply like below formats
Scaffold-DbContext "ConnectionString" Microsoft.EntityFrameworkCore.SQLServer -OutputDir "YourProjectFolderDirectory"

Update
We might need to run or add nuget package - Install-Package Microsoft.EntityFrameworkCore.Tools
Example

Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=SampleDatabase;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir IdentityDataAccess   

Note :
We have stated the output directory to be 'IdentityDataAccess' . This is where all the scaffold classes/entities/context will be placed.
Since the database is SQL database, this script will make use Microsoft.EntityFrameworkCore.SqlServer package to perform such action.

The Result


Conclusion
From the steps and technique in the article, we may conclude that it is extremely easy to generate our ORM mappings to existing database. This can be done by a few line of scripts and probably a little bit of planning ahead on how structure up our data access library

Published on : 1-Feb-2018
Ref no : DDN-WPUB-000216

About Author

My photo
Wan Mohd Adzha MCPD,MCSD,MCSE
I am passionate about technology and of course love Durians. Certified by Microsoft as MCP Since 2011. Blogging from Malaysia

Comments