none
Entity Framework Code First and dependency injection in entity classes

    Question

  • How can I handle dependency injections in entity classes with EF Code First?
    I've looked around bu was only able to find some code that involves ObjectStateManager which is not available in code first (or is it?)

    Here's a code sample to demonstrate the problem

    public class Foo
    {
      public Foo(IDependency dep)
      { ... }
    }

    public class MyContext : DbContext
    {
      IDbSet<Foo> Foos {get;set;}
    }

    when trying to access the repository I get an error from EF that a default constructor cannot be found (that's obvious). I know that I can use property injection instead of constructor injection, but I still need a place in which I can inject the actual object

    Tuesday, June 21, 2011 3:04 PM

Answers

  • Hello,

    you cannot use constructor injection with entity framework. EF always uses the default constructor so it must exist. Your custom constructor will be used only by your code not by EF. There are no extension points allowing you to add support for dependency injection in DbContext API. In case of ObjectContext API you can use ObjectMaterialized event to modify created entity. If you are using any IoC container for dependency injection you can check if it supports method like BuildUp in Unity and use it to resolve property dependencies.

    I'm not sure if you can handle this event in DbContext API but you can try it:

    ObjectContext objectContext = ((IObjectContextAdapter)dbContext).ObjectContext;
    objectContext.ObjectMaterialized += YourHandler;
    
    Generally there are no interception points to modify EF behavior.

    Best regards,
    Ladislav

    • Marked as answer by Krzysiek S Friday, June 24, 2011 7:03 AM
    Wednesday, June 22, 2011 9:04 AM

All replies

  • Hello,

    you cannot use constructor injection with entity framework. EF always uses the default constructor so it must exist. Your custom constructor will be used only by your code not by EF. There are no extension points allowing you to add support for dependency injection in DbContext API. In case of ObjectContext API you can use ObjectMaterialized event to modify created entity. If you are using any IoC container for dependency injection you can check if it supports method like BuildUp in Unity and use it to resolve property dependencies.

    I'm not sure if you can handle this event in DbContext API but you can try it:

    ObjectContext objectContext = ((IObjectContextAdapter)dbContext).ObjectContext;
    objectContext.ObjectMaterialized += YourHandler;
    
    Generally there are no interception points to modify EF behavior.

    Best regards,
    Ladislav

    • Marked as answer by Krzysiek S Friday, June 24, 2011 7:03 AM
    Wednesday, June 22, 2011 9:04 AM
  • Any update? Would you mind letting us know how it goes?

    Please feel free to let us know if you need any further support.

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, June 23, 2011 6:32 AM
  • This seems to work, thanks Ladislav.
    Friday, June 24, 2011 7:04 AM