Model builder configurations

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.


Image

public class MyConfig : EntityTypeConfiguration<MyType>
{
 public MyConfig()
 {
  // your Fluent configurations for this type go in the constructor
 }
}



Image

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


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

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