1

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?

1
  • What is constructor of your EFDbContext class looks like? Did you pass the connection string to it? You could pass a connection string to the DBContext constructor or set the connection string in the web.config file with the name of your context class. blogs.msdn.com/b/adonet/archive/2011/01/27/… Commented Jan 26, 2012 at 23:15

4 Answers 4

2

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" />
Sign up to request clarification or add additional context in comments.

1 Comment

Ok I'll try that when i get home. I forgot to say that my model is in a different project, so its in a separate dll. I think that entity framework, which is working from domain.dll, is not even reading the web.config file in my mvc app. Maybe I need to create an app.config for domain.dll and set my connection string over there.
2

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.

2 Comments

I have an mvc app, which is my startup project, and domain.dll where I have my model. The connection string is in web.config in the mvc app. I think that it's not being read, so EF creates a new database named the same as my context. I'll try to set my connection string in app.config for domain.dll and see what happens.
I ran into this same issue. The configuration needs to be in the startup project.
1

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

Comments

1

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.

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.