In all previous articles, we have seen how we can start with C# code to define database entities and then how migrations can be applied to create the database. Let’s see how EF core can be used with existing database.
In many applications, you may already have a database created with some data in it (let’s say you are working on migrating application to newer technology, but database does not need to be migrated). In such cases, the database schema is already defined, the database is already created.
If we start defining models in C#, that may be very time consuming. A developer may not create the schema exactly as expected, right in the first attempt.
The dotnet CLI can be used to generate a DbContext and EF core models from an existing database. You can refer the CLI documentation here at MSDN.
Now, let’s see how to use it.
Create a database
As we want to demonstrate how to use EF core with existing database, we would need a database.
Download adventure works database backup from MSDN. You can find various database files there, for every version of SQL Server. I downloaded one for SQL Server (Express) 2016, but you can check the SQL Server version you have and then download the appropriate backup file.
Next thing, restore the backup on the SQL server. So, now we have a database with zillions of tables in it.
Create a Demo Project
For using .NET CLI, it needs an existing project in which generated files would be placed after running the scaffold command.
So, let’s create a .NET core class library – an empty one.
Next steps assume that you have already installed DOTNET CLI EF core tools. If not, you can refer this article for step by step instructions.
Now, we need to run the scaffold command. So open the command prompt, and navigate to the directory where CSPROJ file (of the class library we created) is present. Then let’s run the command given below:
First time, when I ran the command I got an error saying the .NET library does not have reference to
Microsoft.EntityFrameworkCore.Design. OK ! I added reference of this NuGet package in the class library. Then again executed the command.
This time, I was expecting the CLI to add reference of
Microsoft.EntityFrameworkCore.Sql. But to my surprise it did not. So, the scaffold command uses the provider specified in the command just to setup the DbContext and models, but it does not add reference to the NuGet package.
So, I had to add reference of NuGet Package –
Microsoft.EntityFrameworkCore.Sql – to the class library.
Then again executed command. This time, all the models and DbContext everything is generated.
All the files are generated directly under the project directory. I personally like to organize all models in directories and have DbContext at the root of project directory. So, if you also like this kind of organized file structure, you would have to do that manually.
All the properties / methods in generated DbContext are virtual. Meaning, if you want to change anything you can derive a new class from the generated one. That way, you can keep the code you written separated from the code that was auto generated.
Another observation, there were some warnings because the scaffolding command could not map some data types. Hence those properties were skipped. So, if you plan to use this option, you should carefully verify that all database entities have all the properties.
The command line also has some options to generate the models only for limited tables, rather than generating them for whole database. You can refer documentation for more options which can be specified in command line.
Thanks you for reading this post. Let me know your thoughts. If you liked this article, do not forget to reshare it and follow me on twitter and GitHub for getting latest updates from the blog.