3

I'm trying to get a very simply EF Code First example running but ran into the above problem. I've followed the advice here ( How to configure ProviderManifestToken for EF Code First ) but to no avail. I finally managed to get EF to create my database and tables by passing the actual connection string to the DBContext instead of the connection string name I had defined.

Here is how I defined by connection string initially in app.config

<connectionStrings>
    <add name="ProductContext" 
         connectionString="integrated security=SSPI;
                           data source=MYMACHINE;
                           persist security info=False;
                           initial catalog=Product"
         providerName="System.Data.SqlClient" />
</connectionStrings>

The name "ProductContext" matches the class ProductContext and the database Product does not exist.

Following the advice on a previous thread I passed the connection string name to ProductContext and the base DBContext cstor. This did not work either.

Finally, I passed the connection string above instead and everything worked, the db was created and the tables.

I'm using SQL Server 2008 and EF 4.1. Any ideas as to what I'm doing wrong?

Thanks,

Ken

Update

The application is a WPF application, not a web application. I get the same exception after I remove the connectionstring from app.config:

"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: 26 - Error Locating Server/Instance Specified)"

Community
  • 1
  • 1
Lance
  • 411
  • 2
  • 6
  • 18
  • Is the connection string in the *app.config* of the project you are running or is it in a library project? – Slauma Sep 16 '11 at 15:22
  • it's in the app.config of the main project not the library. How do a get around that? – Lance Sep 16 '11 at 16:01
  • Tried adding an app.config to the library project and also tried sharing the main projects app.config but neither approach worked. – Lance Sep 16 '11 at 16:09
  • Is it a web application? (Then the connection string must go into *web.config*.) What happens if you remove the connection string completely from the config file? Do you get the same error message or another error? – Slauma Sep 16 '11 at 16:13
  • It's a WPF application. I get the same exception after I remove the connectionstring from app.config. [{"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: 26 - Error Locating Server/Instance Specified)"} – Lance Sep 18 '11 at 20:27
  • I've added your last comment as update to your question to get more reader attention. – Slauma Sep 18 '11 at 21:10
  • I don't have enough knowledge about possible connection strings but I'm working usually with something like this which looks quite different to your string: `connectionString="Server=.\SQLEXPRESS; Database=MyDatabase; Trusted_Connection=Yes;"` Don't know if it's worth a try for you. – Slauma Sep 18 '11 at 21:15
  • Sorry for taking so long in getting back. That connection string didn't make any difference. – Lance Sep 29 '11 at 14:41
  • I gave up on EF and ended up using NHibernate. – Lance Nov 15 '11 at 14:56

2 Answers2

3

I had similar problem on EF6 VS 2013, the problem got solved, when I choose 'Save Password' on Entity Framework - Reverse Engineer Code First - Connection Properties Dialog. Hope this helps someone.

Sivalingaamorthy
  • 983
  • 2
  • 9
  • 26
3

The problem with answering this question is that the culprit could be more than one issue.

That said, I see a couple things to check:

  1. Your Data Source name looks to be invalid. You need to declare the host name and the instance name. Example: .\SQLEXPRESS (notice the . dot) or MyServerHostName\MySqlInstanceName.

    Verify you have a valid data source by using SQL Server Management Studio and trying it there first.

  2. Permissions. You are using integrated security and so the security context of the application making the database connection must have proper rights. Things to check:

    Using SQL Server Mgmt Studio, connect to your database. Select your database in the left pane (Object Explorer) --> Databases --> Select your db --> Security --> Users. Make sure you can verify the account being used to run the app has either been granted rights explicitly or can inherit them by being a member of the groups.

When I had this error, my problem proved to be that I had mixed Integrated Security=SSPI with a "Username =" and "Password =" ...not realizing that I needed Integrated Security=false; my user and password parameters were being ignored.

Not sure about this one: I did read some people suggest (at least for troubleshooting) to set Persist Security Info=true.

shaggy
  • 176
  • 1
  • 5