1

Have put my MVC4/EF5 app on a different machine and now want to generate the database. Have code-first approach and have a seed method in my configuration file although I've lost the code file that generated the database commands in c#. Original solution was VS 2010 but have opened ok in VS 2012 and built and run the home page. As soon as I go to a database-dependent page then I get error.

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)

I have LocalDB 11.0 and 2012 Express installed.

How can I 'reset' so that I can re-create my database on my new machine with my existing code?

I'd put in more code/details but unfortunately not sure which bits are relevant to solving the problem.

UPDATE 1 To get a SQL Express database created I've tried changing the connection string to

<add name="RecruitModelContext" providerName="System.Data.SqlClient" 
connectionString="Server=.\SQLEXPRESS;Initial Catalog=Recruitment;
Integrated Security=True;"/>

Also, I'm calling the following from the Application_Start() in global.asax

using (var db = new RecruitModelContext())
{
    db.Database.Initialize(true);
}

But now I'm getting ProviderManifestToken errors as per other posts. I've tried everything I could find there but not getting a resolution.

UPDATE 2 My connection string was incorrect on the Server name (used fully qualified servername\instancename - had setup different instance name to the default) so it was never seeing my SQL instance.

<add name="RecruitModelContext"
        providerName="System.Data.SqlClient"
        connectionString="Server=MIKEPOOLE72\SQL2012EXPRESS;Initial Catalog=Recruitment;Integrated Security=True;"/>

Removed the initialize code above. Re-ran enable-migrations -Force from the Package Manager Console. Then replaced the Configuration file generated with the one with my Seed method in. Then ran Update-Database from the console and it created my DB with my data.

Thanks to @Code Chops for setting me on my way.

Note I used these MSDN links also which helped on the basics and I'm sure I'll be revisiting these again. http://msdn.microsoft.com/en-US/data/jj556606 http://msdn.microsoft.com/en-us/data/jj592674

Community
  • 1
  • 1
mikepoole72
  • 65
  • 2
  • 9

1 Answers1

0

The error message says your app can't find the database. What's your connection string look like? I'd start by checking that. You can also try something like this in your .config file:

<connectionStrings>
 <add name="MyContext" connectionString="Data Source=|DataDirectory|MyContext.sdf"
      providerName="System.Data.SqlServerCe.4.0" />
</connectionStrings>

Replace where it says MyContext with the name of your DB Context class and it will create a MyContext.sdf file in the AppData directory. To see the file you need to select Show All Files. At least then you'll know your code works.

I'd need to see more code to have a better idea, but it looks like your connection string is the issue. Hope that helps.

UPDATE:

You are currently using the default EF connection factory, which should work... but you might want to change it to name the database by name. Something like this:

<connectionStrings>
<add name="RecruitModelContext" 
    providerName="System.Data.SqlClient" 
    connectionString="Server=.\SQLEXPRESS;Database=YourDatabaseName;Integrated Security=True;"/>
</connectionStrings> 

Make sure you change the Database= part to name your database. Here is a good article about config settings for EntityFramework. Let me know if this helps.

CodeChops
  • 1,980
  • 1
  • 20
  • 27
  • Thanks - that's got my CRUD operations working and I can see the .sdf file which has been generated. What I'd like to do is get an Express db working and populated with my seed data. Will post my Configuration class. – mikepoole72 Sep 27 '13 at 07:33