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

  • SEARCH
  • Home
  • 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 4093146
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:34:52+00:00 2026-05-20T19:34:52+00:00

I’m trying to implement the repository pattern using entity framework code first rc 1.

  • 0

I’m trying to implement the repository pattern using entity framework code first rc 1. The problem I am running into is with creating the DbContext. I have an ioc container resolving the IRepository and it has a contextprovider which just news up a new DbContext with a connection string in a windsor.config file. With linq2sql this part was no problem but EF seems to be choking. I’ll describe the problem below with an example. I’ve pulled out the code to simplify things a bit so that is why you don’t see any repository pattern stuff here. just sorta what is happening without all the extra code and classes.

            using (var context = new PlssContext())
            {
                var x = context.Set<User>();
                var y = x.Where(u => u.UserName == LogOnModel.UserName).FirstOrDefault();
            }

            using (var context2 = new DbContext(@"Data Source=.\SQLEXPRESS;Initial Catalog=PLSS.Models.PlssContext;Integrated Security=True;MultipleActiveResultSets=True"))
            {
                var x = context2.Set<User>();
                var y = x.Where(u => u.UserName == LogOnModel.UserName).FirstOrDefault();
            }

PlssContext is where I am creating my DbContext class. The repository pattern doesn’t know anything about PlssContext. The best I thought I could do was create a DbContext with the connection string to the sqlexpress database and query the data that way. The connection string in the var context2 was grabbed from the context after newing up the PlssContext object. So they are pointing at the same sqlexpress database.

The first query works. The second query fails miserably with this error:

The model backing the ‘DbContext’
context has changed since the database
was created. Either manually
delete/update the database, or call
Database.SetInitializer with an
IDatabaseInitializer instance. For
example, the
DropCreateDatabaseIfModelChanges
strategy will automatically delete and
recreate the database, and optionally
seed it with new data.

on this line

var y = x.Where(u => u.UserName == LogOnModel.UserName).FirstOrDefault();

Here is my DbContext

namespace PLSS.Models
{
    public class PlssContext : DbContext
    {
        public DbSet<User> Users { get; set; }
        public DbSet<Corner> Corners { get; set; }
        public DbSet<Lookup_County> Lookup_County { get; set; }
        public DbSet<Lookup_Accuracy> Lookup_Accuracy { get; set; }
        public DbSet<Lookup_MonumentStatus> Lookup_MonumentStatus { get; set; }
        public DbSet<Lookup_CoordinateSystem> Lookup_CoordinateSystem { get; set; }

        public class Initializer : DropCreateDatabaseAlways<PlssContext>
        {
            protected override void Seed(PlssContext context)
            {

I’ve tried all of the Initializer strategies with the same errors. I don’t think the database is changing. If I remove the

     modelBuilder.Conventions.Remove<IncludeMetadataConvention>();

Then the error returns is

The entity type User is not part of the model for the current context.

Which sort of makes sense. But how do you bring this all together?

  • 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-20T19:34:53+00:00Added an answer on May 20, 2026 at 7:34 pm

    That is correct behavior. Plain DbContext has no idea about mappings (= doesn’t know any of your entities). That is the reason why you should always create derived context. Your repository doesn’t know about PlssContext but you can still inject it like:

    public class Repository
    {
      private readonly DbContext _context;
    
      public Repository(DbContext context)
      {
        _context = context;
      }
      ...
    }
    
    var repository = new Repository(new PlssContext());
    

    You can’t use base DbContext instance directly when using code first.

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

Sidebar

Related Questions

No related questions found

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.