How to get started with EntiyFramework using legacy Database
If you are here to see how you can incorporate Entity Framework in your legacy code base, or you are just curious or simply want to refresh your knowledge of Entity Framework you are in the right place. Focus of this Article will revolve around Package Manager command “Scaffold-DbContext”, its parameters and how they can help you with scaffolding your existing Database to usable C# code. So lets start!
TLDR;
Open Visual Studio, focus on the Project where you want to create your database Entities and Context and run Nuget Package command in format: Scaffold-DbContext ConnectionString Microsoft.EntityFrameworkCore.SqlServer
Example:
Scaffold-DbContext ‘Data Source=.\SQLEXPRESS;Initial Catalog=AdventureWorks2017;Integrated Security=True’ Microsoft.EntityFrameworkCore.SqlServer
In depth explanation:
For testing purposes create C# Console Application and install packages:
Microsoft.EntityFrameworkCore.Tools
Microsoft.EntityFrameworkCore.SqlServer
You can add packages with Package manager console and running following commands;
Install-Package Microsoft.EntityFrameworkCore.Tools
Install-Package Microsoft.EntityFrameworkCore.SqlServer
Following Nuget packages will allow you to run „Scaffold-DbContext“ command which in combination with ConnectionString and other commands will recreate Database entities models and DbContext file
I will be using AdventureWorks2017 Database which can be found here.
Most basic command looks like this Scaffold-DbContext ‘Data Source=.\SQLEXPRESS;Initial Catalog=AdventureWorks2017;Integrated Security=True’ Microsoft.EntityFrameworkCore.SqlServer
By running this command in your project, root of your project will be populated with new cs classes that will represent database Entities, and additional Context class in name-format Database + Context.cs
Of course we can expand this command with multiple options
Generate Context and entities for specific schema
Expand original command with „-Schemas“ command followed by Schema name
Scaffold-DbContext ‘Data Source=.\SQLEXPRESS;Initial Catalog=AdventureWorks2017;Integrated Security=True’ Microsoft.EntityFrameworkCore.SqlServer –Schemas Person, Production
Generate Context and entities for specific Tables
Expand original command with „-Tables“ command followed by Schema name
Scaffold-DbContext ‘Data Source=.\SQLEXPRESS;Initial Catalog=AdventureWorks2017;Integrated Security=True’ Microsoft.EntityFrameworkCore.SqlServer –Tables Address, Person
Change Context class name
If generated context class name is not up to your liking you can easily change it by updating command with „-Context“ parameter
Scaffold-DbContext ‘Data Source=.\SQLEXPRESS;Initial Catalog=AdventureWorks2017;Integrated Security=True’ Microsoft.EntityFrameworkCore.SqlServer — Tables Address, Person –Context MyAwesomeContextName
Change output directories
It really does not seem like a good idea to mix Context class with Entities, luckily we can control that also by using „-ContextDir“ and „-OutputDir“ parameters.
Scaffold-DbContext ‘Data Source=.\SQLEXPRESS;Initial Catalog=AdventureWorks2017;Integrated Security=True’ Microsoft.EntityFrameworkCore.SqlServer -ContextDir Data/Context -OutputDir Data
Which Will create Data folder and add Entities there, and also create Context folder under Data folder and add new Context class there.
These commands will allow you to jump start with Entity Framework development, because it will automatically create Entity classes and most importantly create DbContex file with DbSets and Table configurations for you!
I hope this article has helped you and I wish you happy coding!