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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:18:34+00:00 2026-06-17T06:18:34+00:00

Using EF Code First, I know that I have to match my DbContext class

  • 0

Using EF Code First, I know that I have to match my DbContext class name with the database’s connection string name attribute to make them work together. But the following code does not work:

 public class UserDbContext : DbContext
         {
             public DbSet<Users> Users { get; set; }
         }

along with this connection string:

<add name="UserDbContext" 
 connectionString="Data Source=(LocalDB)\v11.0;
 AttachDbFilename=|DataDirectory|\Users.mdf;
 Integrated Security=True" 
 providerName="System.Data.SqlClient" />

But instead I end up having to use this connection string instead, which points directly to the model along with the DbContext name

<add name="NextFlicksMVC4.Models.userAccount.Users+UserDbContext"     
 connectionString="Data Source=(LocalDB)\v11.0;
 AttachDbFilename=|DataDirectory|\Users.mdf;
 Integrated Security=True"
 providerName="System.Data.SqlClient" />

It works as intended, I connect to a LocalDB in the App_Data Folder.

Can anyone explain to me why it only works when I point it to the full path of the DbContext class?

  • 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-17T06:18:35+00:00Added an answer on June 17, 2026 at 6:18 am

    The default constructor, from the documentation:

    Constructs a new context instance using conventions to create the name
    of the database to which a connection will be made. By convention the
    name is the full name (namespace + class name) of the derived context
    class. For more information on how this is used to create a
    connection, see the remarks section for DbContext.

    From the remarks section for DbContext:

    If the parameterless DbContext constructor is called from a derived
    context, then the name of the derived context is used to find a
    connection string in the app.config or web.config file.

    If you use a decompiler to look at the code for the default constructor in DbContext, you’ll see it calls GetType().ToString() on the DbContext instance and uses that value to find the connection string. So it makes complete sense why the fully-qualified type name of your DbContext is a valid convention to use for naming your connection string.

    In your example, UserDbContext is not the proper name because it is a nested class. You can leave off the namespace but at a minimum you will have to name your connection string Users+UserDbContext. If you want to have the connection string match the class name alone, you can’t nest it inside your model.

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

Sidebar

Related Questions

I have an AXIS2/JAX-WS web service using a code first implementation (yes I know,
I'm using Code First with EF 5. Currently I have SPs that return just
When defining a string in a class using Code First, the default for the
I'm using the Code First approach and have the following Model: public class Person
I'm trying to learn EF using code first, i dont know how to design
Easier to show by example -- I'm using code-first to construct a database. I
I successfully created database on my local server using Code First, migrated it to
I am using EF Code First to create a database on local .\SQLEXPRESS. Among
I'm using the Code First approach to build the database in this problem. I
I'm currently developing a site using Code First and Linq to entities. I Have

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.