Everything we’ve done so far works exactly the same whether you’re using Entity Framework 4 and .NET Framework 4.0 or Entity Framework 5 and .NET Framework 4.5. Data annotations are an exception—they’ve moved.
In versions of Entity Framework prior to 5, some data annotations were declared in the System.ComponentModel.DataAnnotations
namespace (which is part of the .NET class library), but actually in the EntityFramework
assembly. That could be a nuisance, because it required a reference to the Entity Framework in your otherwise pristine, persistence-ignorant code files.
In the .NET Framework 4.5, those annotations are now included in the System.ComponentModel.DataAnnotations
assembly, so that requirement has been removed. That’s a good thing.
But there’s a catch. (Isn’t there always?) Some of the annotations have moved. They’re now in the System.ComponentModel.DataAnnotations.Schema
namespace. You’ll still set the project reference to System.ComponentModel.DataAnnotations
, but your using
or Imports
statement may need to change.
Put On Your Thinking Hat
Using the object browser and the list of data annotations below, can you work out which annotations will require referencing the System.ComponentModel.DataAnnotations.Schema
namespace in your class file?
Column
ComplexType
ConcurrencyCheck
DatabaseGenerated
ForeignKey
InverseProperty
Key
MaxLength
MinLength
NotMapped
Required
StringLength
Table
Timestamp
Put On Your Thinking Hat
How’d you do? Did you find the fact that the annotations are actually named Whatever
Attribute confusing?
On Your Own
There are several ways of dealing with the two-namespace issue. Here are some possibilities. (I’m sure youc can think of others.) They’ll all work. Which one do you think you’ll use?
• Memorize which annotations are contained in each namespace, and only add the references you need.
• Always reference both namespaces in the class files where you want to use annotations.
• Reference System.ComponentModel.DataAnnotations,
but only add the System.ComponentModel.DataAnnotations.Schemas
namespace if you need it. (Visual Studio will fuss at you.)
• Look up the annotations you need to use in the Object Browser before you use them and add the appropriate references.
Put On Your Thinking Hat
Here’s a rather silly little class diagram. Can you write the code and data annotations to change the way Entity Framework will create the schema in the following ways?
• The table name should be MyAnnotatedTable
.
• The property named CallMeJoe
should be represented in the schema as a field called Joe
.
• The maximum length of the ShortString
property should be 5 characters.
• The property named theKey
should be the table’s primary key.
It isn’t absolutely necessary to create a project for this exercise and complete it in Visual Studio (although hands-on experience is always better than pen-to-paper experience when it comes to coding).
If you do, be sure to add the Entity Framework NuGet package and a reference to the System.ComponentModel.DataAnnotations.dll
to it. You’ll also want to add references to both the System.ComponentModel.DataAnnotations
and System.ComponentModel.DataAnnotations.Schema
namespaces (using
in C#, Imports
in VB) to your class file (because you sure don’t want to be typing that over and over again!).
Put On Your Thinking Hat
How’d you do?
[Table("MyAnnotatedTable")]
public class AnnotateMe
{
[Key]
public int theKey { get; set; }
[MaxLength(5)]
public string ShortString { get; set; }
[Column("Joe")]
public int CallMeJoe { get; set; }
}
<Table("MyAnnotatedTable")> _
Public Class AnnotateMe
<Key> _
Public Property theKey as Integer
<MaxLength(5)> _
Public Property ShortString As String
<Column("Joe")> _
Public Property CallMeJoe As Integer
End Class