Once you get past the generic and lambda syntax for entities and properties, the methods exposed by the configuration objects are pretty self-explanatory. Here’s a list of a few of the most common ones. Notice that in addition to the general Property
methods shown, each data type has some methods specific to it. A few of those are included in the list, as well. If you want the complete list, check MSDN for the classes defined in the System.Data.Entity.ModelConfiguration.Configuration
namespace. (Or just use Intellisense. It’s a whole lot easier.)
ModelBuilder.Ignore<TEntity>
Modelbuild.Entity<T>
HasEntitySetName()
HasKey<TProperty>()
ToTable()
HasColumnName()
HasColumnOrder()
HasColumnType()
HasDatabaseGeneratedOption()
IsOptional()
IsRequired()
HasMaxLength()
IsFixedLength()
IsMaxLength()
IsOptional()
IsUnicode()
IsVariableLength()
HasPrecision()
Put On Your Thinking Hat
Can you pick the Fluent method from the list to the left that would perform the following configurations?
Set the database column to be not nullable.
Set the table name to the string provided.
Set the precision of a decimal column in the database.
Exclude a class from the database.
Specify that a property is required.
Set the maximum length of a string column in the database.
Specify the property that is the primary key for a database table.
Specify the data type of a property in the database.
Specify that the value of a property is generated by the database.
Put On Your Thinking Hat
How’d you do?
Set the database column to be not nullable.
IsRequired()
Set the table name to the string provided.
HasEntitySetName()
Set the precision of a decimal column in the database.
HasPrecision()
Exclude a class from the database.
Ignore<TEntity>()
Specify that a property is required.
Set the maximum length of a string column in the database.
HasMaxLength()
Specify the property that is the primary key for a database table.
HasKey<TProperty>
Specify the data type of a property in the database.
HasColumnType()
Specify that the value of a property is generated by the database.
HasDatabaseGeneratedOptions()
Make A Note
Did you notice that the method names look a lot like their corresponding data annotations? I’m sure that’s not an accident, and, in fact, while there are a few things you can do using the Fluent API that aren’t available in data annotations (setting the precision of a decimal field is one example), the only thing you can do in an annotation that you can’t do in the Fluent API is set the minimum length of a string column.
Put On Your Thinking Hat
Ready to give it a try? Make a copy of the three-project solution that we created in the last chapter and use the Fluent API to perform the following configurations:
• Sets the name of the column that represents the Recipe.RecipeName
property to “Name”
• Sets the maximum length of the property to 50
• Sets the property to not nullable
Put On Your Thinking Hat
How’d you do? The code is only a little different from the sample I gave you.
public class RecipeContext : DbContext
{
public DbSet<Recipe> Recipes {get; set;}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Recipe>().Property(t => t.RecipeName)
.HasMaxLength(50)
.IsRequired()
.HasColumnName("Name");
}
}
Public Class FluentContext
Inherits DbContext
Public Property Recipes As DbSet(Of Recipe)
Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
MyBase.OnModelCreating(modelBuilder)
modelBuilder.Entity(Of Recipe)().Property(Function(t) t.RecipeName) _
.HasMaxLength(50) _
.IsRequired() _
.HasColumnName("Name")
End Sub
End Class