1 00:00:00,410 --> 00:00:03,260 Now that we have our entity relationships defined, 2 00:00:03,260 --> 00:00:07,780 we can turn our attention to making some much needed database refinements. 3 00:00:07,780 --> 00:00:11,100 Let's start by taking a look at how we can refine our model 4 00:00:11,100 --> 00:00:16,110 by using data annotation attributes on our entity class properties. 5 00:00:16,110 --> 00:00:21,790 The Artists Name database table column has a data type NVARCHAR (MAX) and 6 00:00:21,790 --> 00:00:24,140 currently allows nulls. 7 00:00:24,140 --> 00:00:29,200 To review, the NVARCHAR data type is a variable length Unicode string. 8 00:00:29,200 --> 00:00:33,340 The MAX part within the parentheses indicates that the column can contain 9 00:00:33,340 --> 00:00:37,090 a string as large as the maximum allowed size, 10 00:00:37,090 --> 00:00:41,760 which is 2 gigabytes or approximately a billion characters. 11 00:00:41,760 --> 00:00:46,627 We can use data annotation attributes to influence how EF will generate the Artists 12 00:00:46,627 --> 00:00:47,680 database table. 13 00:00:51,690 --> 00:00:56,634 As we discussed in an earlier video, EF is using the .NET data types nullability to 14 00:00:56,634 --> 00:00:59,899 determine if the database column should allow nulls. 15 00:00:59,899 --> 00:01:03,100 The .NET string data type is nullable. 16 00:01:03,100 --> 00:01:07,970 So EF will by default make all string-based database columns nullable. 17 00:01:09,070 --> 00:01:12,920 To let EF know that the name property should not allow nulls, 18 00:01:12,920 --> 00:01:16,440 we can decorate it with the required data annotation attribute. 19 00:01:19,337 --> 00:01:21,831 Be sure to add the using directive for 20 00:01:21,831 --> 00:01:26,120 the System.ComponentModel.DataAnnotations namespace. 21 00:01:26,120 --> 00:01:30,560 We can also let EF know what the maximum allowable number of characters 22 00:01:30,560 --> 00:01:33,667 should be for this property by adding the StringLength attribute. 23 00:01:36,882 --> 00:01:41,120 The StringLength attribute accepts some maximum length parameter, which I'll set 24 00:01:41,120 --> 00:01:46,160 to 100, which seems like a reasonable maximum length for this property. 25 00:01:46,160 --> 00:01:49,110 It's worth noting that we can also use the MaxLength 26 00:01:49,110 --> 00:01:51,740 attribute to specify the maximum length. 27 00:01:51,740 --> 00:01:57,850 Unfortunately, the MaxLength attribute doesn't work with ASP.NET MVC validation, 28 00:01:57,850 --> 00:02:00,530 whereas the StringLength attribute does. 29 00:02:00,530 --> 00:02:05,480 So while either would work fine for our console app, I prefer to use StringLength. 30 00:02:05,480 --> 00:02:10,040 Which will make our code more easily ported to an MVC app later on, if or 31 00:02:10,040 --> 00:02:11,180 when we choose to do that. 32 00:02:13,274 --> 00:02:19,440 Let's add the same attributes to the Role entity class' Name property. 33 00:02:32,868 --> 00:02:36,560 And the Series entity class' Title property. 34 00:02:42,240 --> 00:02:44,343 Before we test our changes, 35 00:02:44,343 --> 00:02:49,960 let's look at a few of the other data annotation attributes that we can use. 36 00:02:51,070 --> 00:02:54,470 We already saw how we can change the foreign key property name 37 00:02:54,470 --> 00:02:56,770 by using the foreign key attribute. 38 00:02:56,770 --> 00:03:00,950 In a similar vein, we can customize the name of the database table 39 00:03:00,950 --> 00:03:05,130 by using the Table attribute, Table, 40 00:03:07,777 --> 00:03:13,830 And how about Talent, for our new database table name? 41 00:03:15,708 --> 00:03:18,910 The attribute is in a different namespace. 42 00:03:18,910 --> 00:03:21,370 So we'll need to add a using directive for 43 00:03:21,370 --> 00:03:24,382 the System.ComponentModel.DataAnnotations.Sc- 44 00:03:24,382 --> 00:03:25,297 hema namespace. 45 00:03:28,728 --> 00:03:35,210 We can also customize the name of a column using the Column attribute, Column, 46 00:03:38,870 --> 00:03:42,760 And let's change the name of the column to FullName. 47 00:03:44,793 --> 00:03:49,419 The Table and Column attributes aren't typically used when you're 48 00:03:49,419 --> 00:03:52,680 using EF to generate your database. 49 00:03:52,680 --> 00:03:57,098 But they're useful when working with an existing database, and you want to use 50 00:03:57,098 --> 00:04:01,920 entity class or property names that are different from their associated table or 51 00:04:01,920 --> 00:04:07,809 column names, We can also use the NotMapped 52 00:04:07,809 --> 00:04:12,970 attribute to tell EF to ignore any settable properties on our entity class. 53 00:04:14,140 --> 00:04:17,881 I'll add a string property named Test, 54 00:04:20,145 --> 00:04:24,372 And decorate it with the NotMapped attribute. 55 00:04:24,372 --> 00:04:28,190 EF will automatically ignore any properties that don't have a setter. 56 00:04:28,190 --> 00:04:33,300 So it's redundant to decorate read-only properties with the NotMapped attribute. 57 00:04:33,300 --> 00:04:35,330 Now let's test our model changes. 58 00:04:38,000 --> 00:04:40,500 Our console output still looks like we expect it to. 59 00:04:41,550 --> 00:04:43,370 Let's review the generated database. 60 00:04:47,690 --> 00:04:50,500 If you have the Tables folder open, be sure to refresh it. 61 00:04:53,380 --> 00:04:57,170 Notice that we now have a Talent table instead of an Artists table. 62 00:04:59,880 --> 00:05:04,850 And it contains a FullName column instead of a Name column. 63 00:05:04,850 --> 00:05:11,100 Also the column's data type is now nvarchar(100) instead of nvarchar(MAX). 64 00:05:11,100 --> 00:05:14,350 And the column no longer allows nulls. 65 00:05:14,350 --> 00:05:18,900 And the Series, Title, and Roles Name columns have been updated as well. 66 00:05:28,637 --> 00:05:34,580 And our test property that we added to our Artist entity class was ignored by EF. 67 00:05:36,370 --> 00:05:39,205 Now that we've seen how the Table, Column, and 68 00:05:39,205 --> 00:05:43,170 NotMapped attributes work, let's remove them from our Artist entity class. 69 00:05:50,053 --> 00:05:52,105 We can also remove the using directive for 70 00:05:52,105 --> 00:05:54,788 the System.ComponentModel.DataAnnotations.Sc- 71 00:05:54,788 --> 00:05:56,860 hema namespace, and our test property.