Workflows: Database First6:19 with James Churchill
The process of defining your model and connecting to a database—either existing or new—is referred to as a "workflow". Let's start with an overview of the Database First workflow.
To get started with using entity framework, you need to do two things. 0:00 Describe the shape of your data, and connect to a database. 0:04 Describing the shape of the data is often referred to as data modeling. 0:08 When data modeling, you define the entities, properties on those entities, 0:13 and the relationships between entities. 0:18 For example, in our fitness frog web app from an earlier course, we modeled workout 0:20 data using classes that stored things like the type of activity and 0:25 how long it lasted. 0:30 The data model in EF terminology is known as, the entity data model, 0:32 or EDM, or just the model. 0:38 EF provides a visual designer that allows you to visualize and 0:41 in some cases edit your model. 0:46 We'll see the designer in just a moment. 0:48 If you have an existing database, 0:51 you can use it as a starting point when defining your model. 0:53 If you don't have a database, you can use EF to generate the database. 0:57 The process of defining your model and 1:01 connect it to a database either existing or new is referred to as a workflow. 1:03 EF supports three workflows, Database First, when 1:08 you want to generate your model and entity classes from an existing database. 1:13 Model First when you don't have an existing database and 1:18 you want to define your model using the visual designer and 1:22 allow EF to generate your entity classes and database. 1:26 And Code First which forgoes the visual designer 1:30 by allowing you to define your model by writing the code for your entity classes. 1:34 In this course will be using the most popular of these three options, 1:39 the code first workflow. 1:43 But let's start with an overview of the database first and model first workflows 1:45 so you'll understand at a high level how they compare to each other and 1:50 to the code first workflow. 1:55 Database First workflow was originally the only workflow that EF supported. 1:57 As some name suggests this workflow starts with an existing database. 2:02 After connecting to the database EF generates the model from the database. 2:08 Then the entity and context classes are generated from the model. 2:13 With the Database First workflow, 2:18 changes that need to be made to the model start in the database 2:20 by making schema changes and flow to the application's entity data model. 2:23 This means that the model from the application side is effectively read only. 2:29 I'm going to walk through the steps of creating a model 2:36 using the database first workflow. 2:39 This will be a quick overview. 2:41 So don't worry if you see something that doesn't make complete sense. 2:43 After the next video, we'll be focusing on the Code First workflow 2:47 which I'll explain in detail throughout this course. 2:51 To get started with this workflow, we add in Ado.net into enity data model item 2:53 to our project which will drop us into the Enity Data Model wizard. 2:59 To start, we're asked what the model should contain and were presented with 3:04 four options Entity Designer from database, Empty EF Designer Model, 3:08 Empty Code First model and Code First from database. 3:14 The last two options are for 3:19 the code first workflow which we'll discuss later in the section. 3:21 The second option is for 3:25 the model first workflow which we'll take a look at in the next video. 3:27 And the first option is for 3:32 the database first workflow which is the one that we want for this video. 3:34 The second step allows us to choose our data connection. 3:39 I don't have any existing data connections so I'll create a new connection. 3:42 First, I need to provide the name for my database server. 3:48 I'm using SQL Server local DB, so 3:51 my database server name will start with (localDB), 3:55 followed by a backslash, followed by MSSQLLocalDB. 4:01 In the next section, we'll be taking a closer look at how to install, 4:08 configure and use SQL Server Local DB. 4:12 Now I'll select the database that I want to use. 4:16 DatabaseFirstTestDB, and click OK. 4:21 Now that we've defined our data connection, click Next. 4:28 In the last step, we can select the database objects to include in our model. 4:34 I'll click on the box to the left of the tables item to select all the database's 4:39 tables. 4:43 Click Finish to complete the wizard. 4:44 Now we have an EDMX file in our project. 4:51 Underneath EDMX file we have to code generation templates 4:55 one used to automatically generate the code for 5:00 the indie classes and another to generate the code for the context class. 5:03 We'll take a closer look at the entity and context classes later in the section. 5:08 Code generation templates contain code that generates or writes other code. 5:12 Using code generation to automate the creation of our entity in context classes 5:18 saves us the manual effort creating those classes ourselves. 5:22 When the database schema is updated we can regenerate the EDMX file. 5:26 We can do this by right clicking anywhere in the empty space in the designer and 5:31 selecting update model from database. 5:35 This opens a dialog that will show us a list of the database objects that we can 5:39 add along with the objects that need to be refreshed or deleted. 5:43 Because the database first workflow requires you to start with an existing 5:49 database. 5:53 It's a natural fit when you're updating an existing application to use EF. 5:54 For new applications development developers like having the option to start 5:59 with defining their model from within the application. 6:02 The model first workflow was created for this purpose. 6:06 Up next we'll see how we can use the model first workflow to not only generate 6:10 the entity and context classes from the model but also the database. 6:14
You need to sign up for Treehouse in order to download course files.Sign up