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