Learn Entity Framework#2 | DB-First

Setting up the Database

Step1. Download and install the SQL Server 2019 Express. I left the congifuration to default value.

Step2. Download the SQL Server Management Studio. SSMS is an integrated environment for managing any SQL infrastructure, from SQL Server to Azure SQL Database.

Then, open the SSMS then connect server with the name you just set in step1.

Create database with script file

Step1. Right click databases option in the object explorer, create a new database.

Step2. Open the database script, and execute it on the specific database.

And, we got the database.

Quick Tour of EDMX Designer

Step1. Create a new project of C# console application. (On Visual Studio)

Step2. Add a new item of ADO.NET Entity Data Model. Name it and select "EF Designer from database"

Step3. Set up the connection.

Step4. Change connection setting from {name}Entity to {name}DbContext (for convenience)

Step5. Connect succeeded.

Dealing with Database Changes

Always start with a database, then refresh model.

Adding a new table

  1. Simply use the SSMS to create a new table
  2. Back to visual studio, open the .edmx diagram
  3. Right click on a empty area, select "Update Model from Database..."
  4. Open Update Wizard, which shows any objects in the datebase that we currently do not have in our entity date model.
  5. Finished

Updating and deleting a table

  1. Make some changes like add, modify and delete columns on the SSMS. Save it (if saving not permitted)
  2. Use Update Wizard, same as the last section

!!! Issues you need to be aware of !!! (post on stackoverflow)
Entity Database Model cannot see the changes of updated. You need to manually modify the diagram after you update model.

Working with Enums

First, we should create enum type in our existing code.

namespace DbFirstDemo
    public enum Level : byte
        Beginner = 1,
        Intermediate = 2,
        Advanced = 3
    class Program
        static void Main(string[] args)

Then, go to the Entity model browser. Create a new enum.

  1. The name should be exactly the same as the enum in our code
  2. Left the member empty, because we have create them by code
  3. Reference external type