Updates to – Putting Entity framework 4 to use in a business architecture

This post only reflects some updates to my previous article: Putting Entity framework 4 to use in a business architecture.

I made a “minor” error in my EfDynamicProxyAssemblies that is consumed by my EfDataContractSerializer. The error which I know have corrected arised when I added an example to the client application, where I intended to read back the created wishlist(s) for a certain username. What happened was that the service used the overload of the EfEntityStore’s Query method that lets you specify string’s for memebers in the objectgraph to include in the select. The generated proxie than contained three entities: Wishlist, UserAccount and Wish, but my EfDynamicProxyAssemblies cleared the cached assemblies once the types were extracted. And the first time the dynamic proxie assembly was executed, the only contained type was UserAccount, hence WishList and Wish didn’t get extracted and registrered as a known type.

I have corrected the code and added some information about the custom EfDataContractSerializer attribute in the article.

Download the pdf

The complete code example can be found here.

I have also added some information about “eager loading”. So there is some new code and two new sub chapters.



15 thoughts on “Updates to – Putting Entity framework 4 to use in a business architecture

  1. Pingback: Putting Entity framework 4 to use in a business architecture « Daniel Wertheim

  2. Pingback: DotNetShoutout

  3. Pingback: Entity Framework – Code Only Looking Good (part 2) | Jason Rowe

  4. Hi Daniel,

    Thanks for the update. So far this is the best layered (or tier) architecture i have seen.

    One thing I noticed when I choose IoC scope to Thread-Scope I get an exception if I try to access the EF context second time (even I acess in same function call). I get this execption : “The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.”.
    It is caused by the following code (from your BasicApplicationService.cs)

    using (var entityStore = ServiceEngine.Instance.IoC.Resolve())
    // get something();

    if I call again
    using (var entityStore = ServiceEngine.Instance.IoC.Resolve())
    // get or save something();
    } // error….

    why it happen?

  5. continue the above comment..
    but it will work if I remove using.

    using (var entityStore = ServiceEngine.Instance.IoC.Resolve())
    // get something();


    var entityStore = ServiceEngine.Instance.IoC.Resolve())

    • Hi,

      First of all, I think there are a lot of improvements to do to the architecture, especially how I handle the IoC concept. A better implementation for this could be found: http://daniel.wertheim.se/2010/03/14/linq-to-sql-how-to-separate-the-entities-and-the-datacontext/

      Also note that there is a new version of the CTP (v3), and the article has not been updated for it.

      As for the error you get, I have to look this up. Hopefully during the day. It seems like the Resolve method doesn’t give you a new EntityStore each time, hence the first using-statement will dispose it.


    • ThreadLocal – A single instance will be created for each requesting thread. Caches the instances with ThreadLocalStorage.


  6. Hi Daniel,

    It seems to me that if I use a IoC I should not use using() syntax.

    Example if i set IoC scope as Singleton …
    using (IEntityStore store = IoC.Invoke())
    } // here store call parents dispose so it release its resources example connection.

    Here dispose is the responsibility of IoC not the client. Since we use using .net call disposes method.

    thanks for the project. it’s really wonderful.

  7. Pingback: Entity framework 4 – Code first « Daniel Wertheim

    • Hi,
      It was I while since I sat down with this so I have no answer that comes directly out of my head. I guess it will go faster to google it or check the ADO.Net Team blog. I’m currently starting a project where I will use EF4 code-first so eventually I will have an answer for you.


    • You need to specifically map the relationships:

      Relationship(c => c.Students)
      .FromProperty(student => student.Classes)
      .Map("dbo.ClassRegistrations", (student, class) => new

      The major drawback I found is that both navigation properties must exist in the Domain model i.e. Student.Classes and Class.Students. If Student.Classes doesn’t make sense in your domain, you still need to expose the property. Hopefully this will be fixed in later releases.

      Code first is nice but there are actually quite a lot of constraints placed on the design of classes.

  8. Pingback: Silverlight, WCF, Entity Framework 4 ALL IN ONE « HD BKHN Blog

  9. Pingback: Entity framework 4 – CTP3 – Code first vs Linq to Sql « Daniel Wertheim

  10. I am able to map protected properties using the ObjectAccessor class:


    However, if I wish to specifically Map the table using a similar notation:

    MapSingleType(i => new
    StatusString = ObjectAccessor.CreateExpression("StatusString"),

    I get a runtime exception. Is it possible to specifically use MapSingleType with protected properties?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s