Reputation: 45
I am writing an ASP MVC 3 application. I am using Sql Server 2008 R2 for my database.
I created my data model and my DbContext
called EFDbContext
.
I created my database which is named SportsStore
.
My connection string is:
<add name="EFDbContext" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=SportsStore;Integrated Security=True;Pooling=False" providerName="System.Data.SqlClient" />
I run the app and no data is shown.
I figured out that EF automatically generates a new database SportsStore.Domain.Concrete.EFDbContext
, but I want it to use SportsStore which I have previously created, and to automatically map my model properties to table columns.
If I disable the autogenerate database feature I get the following error:
Cannot open database "SportsStore.Domain.Concrete.EFDbContext" requested by the login
Shouldn't Entity Framework try to open SportsStore database? Why is it trying to open that one? Do I have a mistaken connection string?
Upvotes: 1
Views: 2507
Reputation: 11
I had commented out the default connection string and placed mine underneath:
<connectionStrings>
<!--<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MvcMovie-20130509163734;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-Mvc4Movie-20130509163734.mdf" providerName="System.Data.SqlClient" />-->
<add name="MoveieDBContext" ...
This made EF ignore my connection string and generate its own. Uncommenting the default connection made EF use mine and it all worked.
Upvotes: 1
Reputation: 26
hmm.I was getting same error.after some effort i realized that i had put connection string in wrong Web.config file.There are two web config files.One under view folder and another under the project.You need to add connection string in one under the project folder
Upvotes: 1
Reputation: 7243
I can give you just a suggestion. I came accross with a similar difficulties when I have my DbContext and corresponding connectionstring in one project (usually class library). However it ignores the latter if you use it in other project like win forms. It tries to get the connection string from the startup project's config file.
If this is your case try to place your connection string in your startup project config file.
Upvotes: 2
Reputation: 47375
Strange... have you tried this for your connection string? I think you need MARS to do some things with DbContext.
<add name="EFDbContext" connectionString="Data Source=LOCALHOST\SQLEXPRESS;Initial Catalog=SportsStore;Integrated Security=True;MultipleActiveResultSets=True;" providerName="System.Data.SqlClient" />
Those are the only things different than one of my projects where this works. You are sure the class & connection string are named exactly alike? Did you try naming the connection string like this?
<add name="SportsStore.Domain.Concrete.EFDbContext" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=SportsStore;Integrated Security=True;Pooling=False" providerName="System.Data.SqlClient" />
Upvotes: 2