Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7601307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:08:09+00:00 2026-05-30T23:08:09+00:00

I´ve been checking Microsot Unity IOC and found some examples using Code First approach.

  • 0

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.

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

sorry that I cannot provide code but truly I´m very confused about IOC patterns and I was not able to generate a demo solution.
brgds.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T23:08:11+00:00Added an answer on May 30, 2026 at 11:08 pm

    IoC is simply the turning of an object inside out so that instead of containing internal hard references to objects (dependencies), instead those same objects are passed into it from the outside. The turning inside out is the inversion of control, and the injecting of the objects it needs is the dependency injection and is often done your container (unity).

    All IoC containers are the same, they have some way to register or discover dependencies and then a way to resolve reference. Mostly the resolution involves asking for a reference of FooClass and getting an object in return. Often you don’t actually ask for a concrete type like FooClass and instead ask for an IFooClass so as to decouple your usage from the actual type that gets passed in.

    So in your case you need to register your EF data context as a dependency in unity. I have not used unity before so please forgive any minor errors.

    container.RegisterType<YourContext, YourContext>();
    

    Add a dependency to your class. Say you have a FooRepository that implements IFooRepository.

    public FooRepository : IFooRepository 
    {
        private YourContext context;
    
        public FooRepository(YourDataContext context) {
             this.context = context;
        }
    }
    

    In MVC 3 you register unity as your default dependency resolver which means all requests for controllers are fed through it;

    protected void Application_Start()
    {
        ...
        var container = new UnityContainer();
        container.RegisterType<YourContext, YourContext>();
        container.RegisterType<IFooRepository, FooRepository>();
    
        container.RegisterControllers();
    
        DependencyResolver.SetResolver(new UnityDependencyResolver(container));
    }
    

    Now your controller can add arguments to it’s constructor and have them populated.

    public class MyController : Controller 
    {
        private IFooRepository repository;
    
        public MyController(IFooRepository repository)  {
            this.repository = repository;
        }
    
    }
    

    When this controller get instantiated it will receive an instance of IFooRepository, which will receive a reference to YourContext. This continues all the way down the chain.

    EDIT

    The edmx file is simply a designer that creates a C# context class under the covers. Click on the surface and view the properties to see the name of the class.

    enter image description here

    So you can register it in exactly the same way as any other class. In this case.

    container.RegisterType<Model1Container, Model1Container>();
    

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been checking out some info about 64-bit driver development; I found that
I have been checking out some of the possible timers lately, and System.Threading.Timer and
I have been trying to add columns to a table using some logic that
I have been checking out some Rogue like games (Larn, Rogue, etc) that are
I've been checking out the spinner wheels at http://code.google.com/p/smooth-wheel/ and it's exactly what I'm
I've been checking out Unity and it looks quite interesting. In particular after reading
I have a query I need some help with, have been checking out a
I've been checking/using Twitter Bootstrap for quite a while now. I like what they've
I´ve been checking Kayak.com and many other comparison websites were they create on demand
I've been checking out WCF Data Services today and it looks great - but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.