Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Let's take a look at how we can refine our model by using data annotation attributes on our entity class properties.
Follow Along
To follow along commiting your changes to this course, you'll need to fork the dotnet-comic-book-gallery-model repo. Then you can clone, commit, and push your changes to your fork like this:
git clone <your-fork>
cd dotnet-comic-book-gallery-model
git checkout tags/v3.6 -b using-data-annotations-to-refine-the-generated-database
-
0:00
Now that we have our entity relationships defined,
-
0:03
we can turn our attention to making some much needed database refinements.
-
0:07
Let's start by taking a look at how we can refine our model
-
0:11
by using data annotation attributes on our entity class properties.
-
0:16
The Artists Name database table column has a data type NVARCHAR (MAX) and
-
0:21
currently allows nulls.
-
0:24
To review, the NVARCHAR data type is a variable length Unicode string.
-
0:29
The MAX part within the parentheses indicates that the column can contain
-
0:33
a string as large as the maximum allowed size,
-
0:37
which is 2 gigabytes or approximately a billion characters.
-
0:41
We can use data annotation attributes to influence how EF will generate the Artists
-
0:46
database table.
-
0:51
As we discussed in an earlier video, EF is using the .NET data types nullability to
-
0:56
determine if the database column should allow nulls.
-
0:59
The .NET string data type is nullable.
-
1:03
So EF will by default make all string-based database columns nullable.
-
1:09
To let EF know that the name property should not allow nulls,
-
1:12
we can decorate it with the required data annotation attribute.
-
1:19
Be sure to add the using directive for
-
1:21
the System.ComponentModel.DataAnnotations namespace.
-
1:26
We can also let EF know what the maximum allowable number of characters
-
1:30
should be for this property by adding the StringLength attribute.
-
1:36
The StringLength attribute accepts some maximum length parameter, which I'll set
-
1:41
to 100, which seems like a reasonable maximum length for this property.
-
1:46
It's worth noting that we can also use the MaxLength
-
1:49
attribute to specify the maximum length.
-
1:51
Unfortunately, the MaxLength attribute doesn't work with ASP.NET MVC validation,
-
1:57
whereas the StringLength attribute does.
-
2:00
So while either would work fine for our console app, I prefer to use StringLength.
-
2:05
Which will make our code more easily ported to an MVC app later on, if or
-
2:10
when we choose to do that.
-
2:13
Let's add the same attributes to the Role entity class' Name property.
-
2:32
And the Series entity class' Title property.
-
2:42
Before we test our changes,
-
2:44
let's look at a few of the other data annotation attributes that we can use.
-
2:51
We already saw how we can change the foreign key property name
-
2:54
by using the foreign key attribute.
-
2:56
In a similar vein, we can customize the name of the database table
-
3:00
by using the Table attribute, Table,
-
3:07
And how about Talent, for our new database table name?
-
3:15
The attribute is in a different namespace.
-
3:18
So we'll need to add a using directive for
-
3:21
the System.ComponentModel.DataAnnotations.Sc-
-
3:24
hema namespace.
-
3:28
We can also customize the name of a column using the Column attribute, Column,
-
3:38
And let's change the name of the column to FullName.
-
3:44
The Table and Column attributes aren't typically used when you're
-
3:49
using EF to generate your database.
-
3:52
But they're useful when working with an existing database, and you want to use
-
3:57
entity class or property names that are different from their associated table or
-
4:01
column names, We can also use the NotMapped
-
4:07
attribute to tell EF to ignore any settable properties on our entity class.
-
4:14
I'll add a string property named Test,
-
4:20
And decorate it with the NotMapped attribute.
-
4:24
EF will automatically ignore any properties that don't have a setter.
-
4:28
So it's redundant to decorate read-only properties with the NotMapped attribute.
-
4:33
Now let's test our model changes.
-
4:38
Our console output still looks like we expect it to.
-
4:41
Let's review the generated database.
-
4:47
If you have the Tables folder open, be sure to refresh it.
-
4:53
Notice that we now have a Talent table instead of an Artists table.
-
4:59
And it contains a FullName column instead of a Name column.
-
5:04
Also the column's data type is now nvarchar(100) instead of nvarchar(MAX).
-
5:11
And the column no longer allows nulls.
-
5:14
And the Series, Title, and Roles Name columns have been updated as well.
-
5:28
And our test property that we added to our Artist entity class was ignored by EF.
-
5:36
Now that we've seen how the Table, Column, and
-
5:39
NotMapped attributes work, let's remove them from our Artist entity class.
-
5:50
We can also remove the using directive for
-
5:52
the System.ComponentModel.DataAnnotations.Sc-
-
5:54
hema namespace, and our test property.
You need to sign up for Treehouse in order to download course files.
Sign up