2

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?

6
  • I would Imagine its got something to do with the naming convention. You naming your connection string with the same name as your DbContext. Try chaning the connection name to something else, I think its just coincidence that when your adding the full name that its working. Commented Jan 8, 2013 at 18:57
  • @Derek So if I change my Connection name to FooBar, what exactly do we expect to happen? I assume it will use the default connection as before. Commented Jan 8, 2013 at 19:22
  • Te two connection strings you have listed are identical barring the name. Commented Jan 8, 2013 at 19:33
  • Right, which is the point. In the past as long as the connection name in Web.Config and the dbContext derived class had the exact same name, things were golden. For some reason in this case it is not so. Also in the past my dbContext class has been unable to use any connection entry other than DefaultConnection unless the names match exactly. Commented Jan 8, 2013 at 20:19
  • I have a site set up on Azure CLoud, I'll look at this when i get home. Commented Jan 9, 2013 at 8:58

2 Answers 2

4

Specify a constructor in UserDbContext class like this and will solve the issue

        public UserDbContext() : base("UserDbContext") {}
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.