2

I am working on a website in ASP.NET MVC3 and following the MVC Music Store tutorial to do so, but editing things for what I need. I am now trying to get a preview of the site on my test deploy server but have trouble with the SQL connections.

I altered the SampleData file to fit my needs and replaced DropCreateDatabaseIfModelChanges<MyProjectEntities> with DropCreateDatabaseAlways<MyProjectEntities> because it did not update the sample data when I needed it.

When I try to look up something in the database I get the exception Cannot drop the database 'MyDatabase', because it does not exist or you do not have permission. I understand that this is because I simply cannot drop the database, and I will not be able to do this on my server. So how can I tell the ORM that I don't want it to drop the database to create the tables but rather empty or update an existing database?

Update:

I downloaded SQL Server Management Studio and managed to get the data from the local database to the online database but now I get a different error: Login failed for user '[username]'.

I double checked the connection string and it is correct, here is the full snippet of code from my Web.config:

<add name="AdzTowerEntities"
     connectionString="Data Source=xxxxx.db.xxxxxxx.hostedresource.com; Initial Catalog=xxxxxroot; User ID=xxxxxroot; Password=xxxxxpassword;"
     providerName="System.Data.SqlClient"/>
Cœur
  • 37,241
  • 25
  • 195
  • 267
Kelvin Bongers
  • 187
  • 2
  • 12

1 Answers1

0

What about using MigrateDatabaseToLatestVersion?

If code first migrations are not available to you, then could create a custom IDatabaseInitializer, which will execute the SQL necessary to bring your changes :

 public class AlwaysSeedInitializer : IDatabaseInitializer<MyProjectEntities>
      {
         public void InitializeDatabase( MyProjectEntities context )
             {
                if( context.Database.Exists() )
                {
                   if( !context.Database.CompatibleWithModel( true ) )
                   {
                      context.Database.SqlCommand("Add SQL commands here");
                   }
                }

                if ( /* Insert test to see if database is already seeded */ )
                {
                   //Seed here if the db is not seeded 
                }
             }
      }

And put that in your Application_Start() of your global.asax:

 Database.SetInitializer(new AlwaysSeedInitializer());
Mark Oreta
  • 10,346
  • 1
  • 33
  • 36
  • The problem is not that I don't want it to drop, the problem is that I can not make it drop. The MSSQL server I'm on forces me to use the online interface to create and drop databases and does not allow MSSQL accounts created with it to do deleting or creating of databases. – Kelvin Bongers Aug 04 '12 at 18:52
  • My apologies, I misunderstood your question. I've updated my response, does this help? – Mark Oreta Aug 04 '12 at 19:07
  • That class does not seem to be available. – Kelvin Bongers Aug 04 '12 at 19:12
  • Would you be able to use [code first migrations](http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-code-based-migrations-walkthrough.aspx) and entity framework 4.3? – Mark Oreta Aug 04 '12 at 19:17
  • I think I myself misunderstood where the problem is located, I use a local SQL Server Compact Edition for development, and I think I might be able to fix it by simply moving the data from the local database to the production server. I can't however for the life of me find out how I would do that. – Kelvin Bongers Aug 04 '12 at 19:28
  • You can try changing the connectionsting in your web.config or, you can pass in a new one when you construct the db. See [this post](http://stackoverflow.com/questions/4805094/pass-connection-string-to-code-first-dbcontext) – Mark Oreta Aug 04 '12 at 19:31
  • Yes, I changed the connection string, but my hosting provider does not allow me to drop or create databases, so I will have to transfer the data from the local database manually so it won't have to. – Kelvin Bongers Aug 04 '12 at 19:35
  • If you can't create a database, how will you move the data there? Can you execute sql commands at all, possibly through a web interface? Is so, you can use Code First Migrations, it can generate the SQL scripts for you see the bottom of [this article](http://blogs.msdn.com/b/adonet/archive/2012/01/12/ef-4-3-beta-1-code-based-migrations-walkthrough.aspx) – Mark Oreta Aug 04 '12 at 19:38
  • My project can run SQL commands fine, the only thing I can't do is create or drop a database. I'm on a GoDaddy Windows Hosting account. I currently have a sql dump of my local database, I just don't know how to run the entire file at once on my server. – Kelvin Bongers Aug 04 '12 at 19:43
  • I updated the question to show the updates in what situation I'm trying to resolve. – Kelvin Bongers Aug 04 '12 at 20:28