The Fluent API is pretty easy to use (once you get the hang of all those lambdas), but the OnModelBuilding()
method can quickly get pretty messy if you configure everything in the same method. Fortunately, the DbModelBuilder
exposes a collection property, Configurations
, that lets you separate the configurations for each type in a single class.
Just create a class that inherits from EntityTypeConfiguration<TEntity>
, where TEntity
is the type you want to configure.
public class MyConfig : EntityTypeConfiguration<MyType>
{
public MyConfig()
{
// your Fluent configurations for this type go in the constructor
}
}
Public Class MyConfig
Inherits EntityTypeConfiguration(Of MyType)
Public Sub New()
'your Fluent configurations for this type go in the constructor
End Sub
End Class
And then add an instance of the class to the modelBuilder.Configurations
property inside your OnModelCreating()
method.
modelBuilder.Configurations.Add(new MyConfig())