Fluent methods

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 methods

ModelBuilder.Ignore<TEntity>
Modelbuild.Entity<T>

 

Entity methods

HasEntitySetName()
HasKey<TProperty>()
ToTable()

 

General property methods

HasColumnName()
HasColumnOrder()
HasColumnType()
HasDatabaseGeneratedOption()
IsOptional()
IsRequired()

String property methods

HasMaxLength()
IsFixedLength()
IsMaxLength()
IsOptional()
IsUnicode()
IsVariableLength()

 

Decimal property methods

HasPrecision()


Image 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.

 



Image 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.

Image

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()



Image 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.



Image 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



Image Put On Your Thinking Hat

How’d you do? The code is only a little different from the sample I gave you.

Image

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");
 }
}


Image

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



..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset