2

I have set up a Code First using the following DbContext

public class MyDatabaseDbContext : DbContext { public MyDatabaseDbContext(string nameOrConnectionString) : base(nameOrConnectionString) { } public MyDatabaseDbContext() : base("MyDatabase") { } public DbSet<MyTable> MyTables { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); } } 

I have also set up the connection string to look at the (Local) database

<connectionStrings> <add name="MyDatabase" connectionString="Server=(local);Database=MyDatabase;Trusted_Connection=True;MultipleActiveResultSets=true" providerName="System.Data.SqlClient" /> </connectionStrings> 

Also set up the initializer in Global.asax

Database.SetInitializer<MyDatabaseDbContext>(new DropCreateDatabaseAlways<MyDatabaseDbContext>()); 

I then run the application and call the DbContext (in my case from the controller)

 var dbContext = new MyDatabaseDbContext(); var myTableResults = dbContext.MyTables.ToList(); 

I get the follow error message

An exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.dll but was not handled in user code

Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Cannot get a local application data path. Most probably a user profile is not loaded. If LocalDB is executed under IIS, make sure that profile loading is enabled for the current user.

On Debugging and looking at the dbContext variable > Database > Connection > ConnectionString Property I see the following: "Data Source=(localdb)\v12.0;AttachDbFilename=|DataDirectory|MyDatabase.mdf;Initial Catalog=MyDatabase;Integrated Security=True;MultipleActiveResultSets=True"

Questions

Why is it pointing to the (localDb)\v12.0 database and not my connection string? Even so, Why is it not creating the database there anyway as this is on my dev machine?

Is there some kind of convention which I have forgotten about which I need to set.

1
  • try renaming your db file. Commented May 10, 2016 at 13:51

1 Answer 1

3

Your connection string needs to be named MyDatabaseDb OR your context class needs to be named MyDatabaseContext.

Entity Framework's convention is for the context class to look for a connection string with the same name + Context. The Db in your class name is extraneous.

Sign up to request clarification or add additional context in comments.

2 Comments

I've upvoted you as you pointed me in the right direction. I had not named them to that convention and on changing it, it was pointing SQLEXPRESS. I've fallen for this before and I now have this SO to remind me:). There was a few other issues. I had updated the connection string in the database class library and not in the web app where I was calling it from. The SQLEPRESS issue was the default connectionString in the website which was soon replaced with my own. Thanks @jason-watts
No problem. Been there myself a few times.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.