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 8442253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:48:07+00:00 2026-06-10T08:48:07+00:00

I have recently been learning MVC3 using Entity Framework 4.3 and Dependancy Injection so

  • 0

I have recently been learning MVC3 using Entity Framework 4.3 and Dependancy Injection so I can implement unit tests at a later date. I am now trying to implement some functions I have seen in various examples however I have hit a few issues that seem to stem from my use of dependancy injection and therefore I would like someone to point out where I am going wrong.

My first question is pretty simple; In most MVC3 examples I have seen, the access to the database is done in controllers, but when using dependancy injection I seem to need to refactor this code out to the implemented repository. Is this normal and correct?

My second more indepth issue is dealing with this simple example:

I have this method in my repository class which is pretty much copied from an online example (here). However I get an error regarding the Include part, and the intellisense does say that the variable needs to be a string. I have tried the original code from the link I mentioned earlier and that works fine, the only major difference between the project is that I am using dependancy injection.

public ExampleUser GetStruContractUser(int id)
{
    ExampleUser user = context.ExampleUsers
        .Include(i => i.ExampleRoles)
        .Where(i => i.UserID == id)
        .Single();

    return user;
}

Changing the Include parameter to the following works fine.

public ExampleUser GetStruContractUser(int id)
{
    ExampleUser user = context.ExampleUsers
        .Include("ExampleRoles")
        .Where(i => i.UserID == id)
        .Single();

    return user;
}

For reference this is my DbContext class that I am using:

public class EFDbContext : DbContext
{
    public DbSet<ExampleUser> ExampleUsers { get; set; }
    public DbSet<ExampleRole> ExampleRoles { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        modelBuilder.Entity<ExampleUser>().ToTable("User", "MySchema");
        modelBuilder.Entity<ExampleRole>().ToTable("Role", "MySchema");

        modelBuilder.Entity<ExampleUser>()
            .HasMany(m => m.ExampleRoles)
            .WithMany(t => t.ExampleUsers)
            .Map(a =>
            {
                a.MapLeftKey("UserID");  // your PK column name in user table
                a.MapRightKey("RoleID"); // your PK column name in role table
                a.ToTable("UserRole", "MySchema");  // your join table name
            });
        }
    }

Is this an issue due to my use of dependancy injection or is there something else going on that I am misunderstanding?

If you need more information, please ask and I will try and provide it.

Thanks very much.

  • 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-06-10T08:48:08+00:00Added an answer on June 10, 2026 at 8:48 am

    Yes, extracting your database layer (repositories / context) and then injecting it into your business layer (controllers) is very normal and a good way future proof your application. What if you ever wanted to change your database ORM from Entity Framework to something else? Having it tightly coupled with your controllers would create a huge headache.

    Your second issue, with include, is unrelated to the dependancy injection.

    Include on a dbset using lambdas an extension in System.Data.Entity. You’ll need to include that reference if you want to use it.

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

Sidebar

Related Questions

I have recently been using junit in eclipse and I am still learning. I
I have very recently been learning python. I have been using pyscripter as is
So I've recently started learning Scala and have been using graphs as sort of
I have been learning MVC recently and have started creating my own framework (for
I've recently been learning jQuery Mobile but have a hard time remembering all the
Recently I have been learning about WMI and WQL. I found out the list
I have been learning NodeJS recently. It seems to be standard in the Node
I have recently been using WxPython to create a GUI Network simulator like Cisco
I have recently been learning more about design patterns and thought I'd take a
I have recently been getting my feet wet in MongoDB using Mongoid w/ Rails

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.