Search code examples
entity-framework-4ef4-code-only

From what information is EF4 Code first building the conceptual model


As I understand Code First is building a model to work with EF4 in memory and where you can fine tune your mappings and stuff.

Based on what information is this model being build?

You see I have an existing application and am looking to change our DAL to code first, but I want to do it piece by piece. So I have a Context class like this:

Public Class JournalContext
    Inherits DbContext

    Public Sub New()
        MyBase.New("AppDb")
    End Sub

    Public Property JournalEntries As IDbSet(Of JournalEntry)

    Protected Overrides Sub OnModelCreating(ByVal modelBuilder As System.Data.Entity.ModelConfiguration.ModelBuilder)

        modelBuilder.Entity(Of JournalEntry).Property(Function(e) e.Id).HasColumnName("JournalEntryId")
    End Sub

End Class

EF is complaining when I use this class about other Entity types that has no key defined. But I don't want them to be in my model for EF.


Solution

  • It takes information from fluent mapping and mapped entity itself. So if your JournalEntity contains properties which returns other entities, I think EF will try to map them unless you explicitly exclude them from mapping.