Search code examples
c#vb.netef-code-firstentity-framework-6

What is modelBuilder.entity(Of x)


I have a context class for an EF 6 project that has the following modelBuilder properties for each column in each table:

Protected Overrides Sub OnModelCreating(ByVal modelBuilder As DbModelBuilder)
    modelBuilder.Entity(Of Product)() _
        .Property(Function(e) e.Name) _
        .IsUnicode(False)

    modelBuilder.Entity(Of Product)() _
        .Property(Function(e) e.Description) _
        .IsUnicode(False)
End Sub

What is this for? In database first or even code first examples I have not seen this. This context class was created with 'code first existing database'. I'm new to EF and would like to understand what this is present for.

Thanks-


Solution

  • When using Code First your model is calculated from your classes using a set of conventions. The default Code First Conventions determine things like which property becomes the primary key of an entity, the name of the table an entity maps to, and what precision and scale a decimal column has by default.

    Sometimes these default conventions are not ideal for your model, and you have to work around them by configuring many individual entities using Data Annotations or the Fluent API. Custom Code First Conventions let you define your own conventions that provide configuration defaults for your model.

    Read more here...

    https://msdn.microsoft.com/en-us/data/jj819164.aspx

    Nice article as well...

    http://www.codeproject.com/Articles/165720/Using-the-Code-First-Model-Configuration-Classes