locked
Unity IOC - Registering EF data context as a dependency RRS feed

  • Question

  • User479136567 posted

    Hi All,

    I´ve been checking Microsot Unity IOC and found some examples using Code First approach. On the other hand I cannot find any tutorial or configuration in order to include Unity IoC with edmx files using a database first approach. I will be glad in anyone could shed some light on it.

    Im using http://unitymvc3.codeplex.com/ and using unity 2.1 directly = http://unity.codeplex.com/

    I tried to register my EF data context as a dependency in unity. I have not used unity before so please forgive any minor errors. I tried to do it using and approach like:

    container.RegisterType<YourContext>(); 

    using the entity container name as <YourContext> -where I click on the surface and view the properties of the edmx file and took that value-  but with no success.

    Is this the correct approach?, its ok to register the edmx file as a container.registertype?... thanks in advance.

    brgds

    sebastian.

     

    Wednesday, March 7, 2012 11:51 PM

Answers

  • User479136567 posted

    Perfect, I found what happened. I changed edmx file properties to the following details and it was recongnized in container.registertype: 1- Code generation move to: Default 2- lazyloading : false and it worked. Additionally paul hiles of unity.mvc3 responded:

    "Most of the examples use DbContext as you say but there is nothing in Unity.Mvc3 that ties you to using Code First. For EF with edmx files, you will have an auto-generated ObjectContext implementation instead of a DbContext. This implements IDisposable so needs to be treated in the same way as a DbContext. This means that you should register it with Unity using the HierarchicalLifetimeManager. e.g.
    container.RegisterType<ObjectContext, MyDbEntities>(         new HierarchicalLifetimeManager());"

    brgds

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 8, 2012 1:50 PM