Entity Framework 4.1 – DbContext Data Access

Entity Framework 4.1 – DbContext Data Access covers the DbContext object. It is important. The lecture explains why it is important. It might seem abstract if you haven’t touched any of this stuff before. That is OK. You will recall it when you need it.
Personal notes follow.


1 Part 1: DbContext stuff

  • DbContext, DbSet, DbQuery
  • Binding to State initates entity tracking by the context
  • Find pulls first from the entity cache, so beware unexpected out of date reads
  • What is up with the EF “check for one result” in every query check?!
  • Local property holds object references for those managed by the context
  • ObservableCollections stay in sync with objects tracked by the context
  • Things just got serious, DynamicProxies for lazy-loading and/or change-tracking
  • Change-tracking is disabled when non-virtual properites are present in a class
  • You can disabled change tracking
    • Who cares about changes?
      • DbSet: Find, Local, Remove, Add
      • DbContext: SaveChanges, GetValidationErrors, Entry
      • DbChangeTracket: Entries
  • You might care about auto proxy generation in scenario of repositories that are
    used both in the back end, which would want dynamic proxies, and on services,
    which would want to return real objects not proxies.
  • Wondered why we want access to the DbEntityEntries and Context. Good2know.
  • Context.Aliases.Load pulls the aliases back, but no their entities
  • You can work with the objects managed by the change tracker without
    referencing them by type. You examine them using property names, so you don’t
    have to worry about types.
  • You may easily stomp on aliases by accidentally “merging” them onto an
    existing alias
  • Can do server-wins with a call to Reload
  • Arthur Vickers: Using DbContext in EF 4.1 Series

2 Part 2: Code First Stuff

  • CF generates a default connection string in absence of one
  • IDatabaseInitializer
    • If it doesn’t exist
    • If the model changes
    • Always recreate
    • Your custom approach
  • Red Gate .NET Reflector
  • Watch the Migrations lecture next
  • Database.SetInitializer
    • Just use existing, don’t use ’em
  • CLR notation for XML
  • System.Data.Entity.Database has all the stuff in it to work right with it

One thought on “Entity Framework 4.1 – DbContext Data Access”

Leave a Reply

Your email address will not be published. Required fields are marked *