1 00:00:00,500 --> 00:00:04,870 To get started with using entity framework, you need to do two things. 2 00:00:04,870 --> 00:00:07,490 Describe the shape of your data, and connect to a database. 3 00:00:08,570 --> 00:00:13,030 Describing the shape of the data is often referred to as data modeling. 4 00:00:13,030 --> 00:00:18,210 When data modeling, you define the entities, properties on those entities, 5 00:00:18,210 --> 00:00:20,980 and the relationships between entities. 6 00:00:20,980 --> 00:00:25,980 For example, in our fitness frog web app from an earlier course, we modeled workout 7 00:00:25,980 --> 00:00:30,340 data using classes that stored things like the type of activity and 8 00:00:30,340 --> 00:00:31,350 how long it lasted. 9 00:00:32,440 --> 00:00:38,000 The data model in EF terminology is known as, the entity data model, 10 00:00:38,000 --> 00:00:41,670 or EDM, or just the model. 11 00:00:41,670 --> 00:00:46,320 EF provides a visual designer that allows you to visualize and 12 00:00:46,320 --> 00:00:48,960 in some cases edit your model. 13 00:00:48,960 --> 00:00:51,690 We'll see the designer in just a moment. 14 00:00:51,690 --> 00:00:53,870 If you have an existing database, 15 00:00:53,870 --> 00:00:57,070 you can use it as a starting point when defining your model. 16 00:00:57,070 --> 00:01:01,890 If you don't have a database, you can use EF to generate the database. 17 00:01:01,890 --> 00:01:03,715 The process of defining your model and 18 00:01:03,715 --> 00:01:08,935 connect it to a database either existing or new is referred to as a workflow. 19 00:01:08,935 --> 00:01:13,940 EF supports three workflows, Database First, when 20 00:01:13,940 --> 00:01:17,800 you want to generate your model and entity classes from an existing database. 21 00:01:18,960 --> 00:01:22,570 Model First when you don't have an existing database and 22 00:01:22,570 --> 00:01:26,130 you want to define your model using the visual designer and 23 00:01:26,130 --> 00:01:30,400 allow EF to generate your entity classes and database. 24 00:01:30,400 --> 00:01:34,070 And Code First which forgoes the visual designer 25 00:01:34,070 --> 00:01:37,950 by allowing you to define your model by writing the code for your entity classes. 26 00:01:39,140 --> 00:01:43,310 In this course will be using the most popular of these three options, 27 00:01:43,310 --> 00:01:45,170 the code first workflow. 28 00:01:45,170 --> 00:01:50,340 But let's start with an overview of the database first and model first workflows 29 00:01:50,340 --> 00:01:55,150 so you'll understand at a high level how they compare to each other and 30 00:01:55,150 --> 00:01:56,350 to the code first workflow. 31 00:01:57,660 --> 00:02:02,860 Database First workflow was originally the only workflow that EF supported. 32 00:02:02,860 --> 00:02:08,130 As some name suggests this workflow starts with an existing database. 33 00:02:08,130 --> 00:02:13,110 After connecting to the database EF generates the model from the database. 34 00:02:13,110 --> 00:02:17,090 Then the entity and context classes are generated from the model. 35 00:02:18,130 --> 00:02:20,140 With the Database First workflow, 36 00:02:20,140 --> 00:02:23,960 changes that need to be made to the model start in the database 37 00:02:23,960 --> 00:02:29,630 by making schema changes and flow to the application's entity data model. 38 00:02:29,630 --> 00:02:34,770 This means that the model from the application side is effectively read only. 39 00:02:36,550 --> 00:02:39,510 I'm going to walk through the steps of creating a model 40 00:02:39,510 --> 00:02:41,730 using the database first workflow. 41 00:02:41,730 --> 00:02:43,400 This will be a quick overview. 42 00:02:43,400 --> 00:02:47,340 So don't worry if you see something that doesn't make complete sense. 43 00:02:47,340 --> 00:02:51,260 After the next video, we'll be focusing on the Code First workflow 44 00:02:51,260 --> 00:02:53,980 which I'll explain in detail throughout this course. 45 00:02:53,980 --> 00:02:59,180 To get started with this workflow, we add in Ado.net into enity data model item 46 00:02:59,180 --> 00:03:04,284 to our project which will drop us into the Enity Data Model wizard. 47 00:03:04,284 --> 00:03:08,780 To start, we're asked what the model should contain and were presented with 48 00:03:08,780 --> 00:03:14,670 four options Entity Designer from database, Empty EF Designer Model, 49 00:03:14,670 --> 00:03:19,340 Empty Code First model and Code First from database. 50 00:03:19,340 --> 00:03:21,100 The last two options are for 51 00:03:21,100 --> 00:03:25,500 the code first workflow which we'll discuss later in the section. 52 00:03:25,500 --> 00:03:27,610 The second option is for 53 00:03:27,610 --> 00:03:32,140 the model first workflow which we'll take a look at in the next video. 54 00:03:32,140 --> 00:03:34,090 And the first option is for 55 00:03:34,090 --> 00:03:37,840 the database first workflow which is the one that we want for this video. 56 00:03:39,460 --> 00:03:42,860 The second step allows us to choose our data connection. 57 00:03:42,860 --> 00:03:46,910 I don't have any existing data connections so I'll create a new connection. 58 00:03:48,410 --> 00:03:51,800 First, I need to provide the name for my database server. 59 00:03:51,800 --> 00:03:55,736 I'm using SQL Server local DB, so 60 00:03:55,736 --> 00:04:01,983 my database server name will start with (localDB), 61 00:04:01,983 --> 00:04:08,280 followed by a backslash, followed by MSSQLLocalDB. 62 00:04:08,280 --> 00:04:12,970 In the next section, we'll be taking a closer look at how to install, 63 00:04:12,970 --> 00:04:15,680 configure and use SQL Server Local DB. 64 00:04:16,820 --> 00:04:19,570 Now I'll select the database that I want to use. 65 00:04:21,914 --> 00:04:28,130 DatabaseFirstTestDB, and click OK. 66 00:04:28,130 --> 00:04:31,340 Now that we've defined our data connection, click Next. 67 00:04:34,220 --> 00:04:38,300 In the last step, we can select the database objects to include in our model. 68 00:04:39,660 --> 00:04:43,350 I'll click on the box to the left of the tables item to select all the database's 69 00:04:43,350 --> 00:04:44,450 tables. 70 00:04:44,450 --> 00:04:46,060 Click Finish to complete the wizard. 71 00:04:51,625 --> 00:04:55,272 Now we have an EDMX file in our project. 72 00:04:55,272 --> 00:05:00,640 Underneath EDMX file we have to code generation templates 73 00:05:00,640 --> 00:05:03,110 one used to automatically generate the code for 74 00:05:03,110 --> 00:05:08,410 the indie classes and another to generate the code for the context class. 75 00:05:08,410 --> 00:05:12,860 We'll take a closer look at the entity and context classes later in the section. 76 00:05:12,860 --> 00:05:17,290 Code generation templates contain code that generates or writes other code. 77 00:05:18,450 --> 00:05:22,930 Using code generation to automate the creation of our entity in context classes 78 00:05:22,930 --> 00:05:26,670 saves us the manual effort creating those classes ourselves. 79 00:05:26,670 --> 00:05:31,320 When the database schema is updated we can regenerate the EDMX file. 80 00:05:31,320 --> 00:05:35,740 We can do this by right clicking anywhere in the empty space in the designer and 81 00:05:35,740 --> 00:05:38,270 selecting update model from database. 82 00:05:39,820 --> 00:05:43,920 This opens a dialog that will show us a list of the database objects that we can 83 00:05:43,920 --> 00:05:47,840 add along with the objects that need to be refreshed or deleted. 84 00:05:49,880 --> 00:05:53,390 Because the database first workflow requires you to start with an existing 85 00:05:53,390 --> 00:05:54,340 database. 86 00:05:54,340 --> 00:05:59,040 It's a natural fit when you're updating an existing application to use EF. 87 00:05:59,040 --> 00:06:02,880 For new applications development developers like having the option to start 88 00:06:02,880 --> 00:06:06,570 with defining their model from within the application. 89 00:06:06,570 --> 00:06:10,230 The model first workflow was created for this purpose. 90 00:06:10,230 --> 00:06:14,710 Up next we'll see how we can use the model first workflow to not only generate 91 00:06:14,710 --> 00:06:18,520 the entity and context classes from the model but also the database.