iT邦幫忙

0

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.
https://ithelp.ithome.com.tw/upload/images/20200528/20127443XWEIuQF1KP.png


Create database with script file

Step1. Right click databases option in the object explorer, create a new database.
https://ithelp.ithome.com.tw/upload/images/20200529/20127443LBze0AJ8qu.png

Step2. Open the database script, and execute it on the specific database.
https://ithelp.ithome.com.tw/upload/images/20200529/20127443XRVEr4tqsc.png

And, we got the database.
https://ithelp.ithome.com.tw/upload/images/20200529/20127443gJ8vOxguHM.png


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"
https://ithelp.ithome.com.tw/upload/images/20200529/20127443cAEPCk8kS1.png

Step3. Set up the connection.
https://ithelp.ithome.com.tw/upload/images/20200529/20127443CH7UrmZETn.png

Step4. Change connection setting from {name}Entity to {name}DbContext (for convenience)
https://ithelp.ithome.com.tw/upload/images/20200529/20127443kkZ3VH9O7u.png

Step5. Connect succeeded.
https://ithelp.ithome.com.tw/upload/images/20200529/20127443NhpbrKGCSC.png


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.
    https://ithelp.ithome.com.tw/upload/images/20200529/201274438pDSeIfu6M.png
  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

https://ithelp.ithome.com.tw/upload/images/20200531/20127443Pt0uB6w9Ds.png


尚未有邦友留言

立即登入留言