0

I'm using Code First with Entity Framework 5 using MVC4 and MVC Scaffolding (link/tutorial: http://www.codeproject.com/Articles/468777/Code-First-with-Entity-Framework-5-using-MVC4-and?msg=4531106#xx4531106xx) to create my database from my models. So, basically, when I run the app, the app drops and creates a new database if my model changes. That's great. Now, obviously, this is great for local development.

But what about when my app goes to production? How do I tell the code to NOT do this anymore, and rather point my app to use the database I have hosted on GoDaddy? I changed the connection string in the web.config to point to that of the GoDaddy database, but this isn't working. What happens is the code still tries to use the old connection string for some reason unknown to me. I can't seem to get my app to NOW use the database I created on GoDaddy. I'm starting to regret going down the code-first route....

Here's the line of code in my Application_Start method that auto-generates my database (in this case, only if my model changes):

System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges<CapWorx.QuikCap.Models.CapWorxQuikCapContext>());
Mike Marks
  • 10,017
  • 17
  • 69
  • 128
  • System.Data.Entity.Database.SetInitializer(null); It won't modify database structure in any way. – Lars Apr 02 '13 at 15:38
  • Can't pass null in as an argument, unfortunately... – Mike Marks Apr 02 '13 at 16:00
  • Yes, you can. Not sure why you think you can't. – Chris Pratt Apr 02 '13 at 16:20
  • what's your connection string in the config (under `connectionStrings`) - and do you have ctor in `CapWorxQuikCapContext` passing the connection string? It seems a connection issue – NSGaga-mostly-inactive Apr 02 '13 at 17:28
  • @ChrisPratt As it sits right now, if I explicitly type: System.Data.Entity.Database.SetInitializer(null) -- this comes back as a compile time error. The type arguments cannot be inferred from the usage. – Mike Marks Apr 02 '13 at 18:42
  • @NSGaga My connection string in my web.config is the connection string pointed to my GoDaddy database. Your inquiry about the constructor- this is where my weak spot is. I'm not sure what to put in my constructor in my data context. I have static CapWorxQuikCapContext() { } And nothing is in it. What's your guidance from here as far as how to pass the connection string in my constructor? – Mike Marks Apr 02 '13 at 18:47
  • simple rule is - it's not about static ctor - if you have the 'normal' CapWorxQuikCapContext() : base("connection") - then you have to put that name in the config as well. If you don't - then you have to name the connection string `the same` as your Context class - i.e. exactly the `CapWorxQuikCapContext`. If you don't - EF/CF makes its own 'connection' path - i.e. making some default db name - like your context - or like you have in the ctor. – NSGaga-mostly-inactive Apr 02 '13 at 19:06
  • @NSGaga Thanks. I actually discovered exactly what you said at the same time you posted this comment. Please feel free to put that into an answer below and I'll mark it as the answer. – Mike Marks Apr 02 '13 at 19:22
  • no problem - I just made an answer, cheers – NSGaga-mostly-inactive Apr 02 '13 at 19:27

2 Answers2

1

I found the solution to my problem.

Originally, I had the following in my context's constructor:

static CapWorxQuikCapContext()
{
    Database.SetInitializer(new CodeFirstSeeder.Xml.DropCreateDatabaseAlways<CapWorxQuikCapContext>());
}

Note that the CodeFirstSeeder is irrelevant at this point- it's just a strategy I installed to seed some sample data. Anyways, I changed the above to be:

public CapWorxQuikCapContext()
    : base("DefaultConnection")
{
}

This tells my app to look at the connection string called "DefaultConnection" and use it for its data source.

Mike Marks
  • 10,017
  • 17
  • 69
  • 128
0

(as per our discussion / comments)

simple rule is -

If you have the 'non-static ctor' CapWorxQuikCapContext() : base("connection") - then you have to use that same name in the config as well.

Otherwise - you have to name the connection string the same as your Context class - i.e. exactly the CapWorxQuikCapContext.

If you don't - EF/CF makes its own 'connection' path - i.e. making some default db name - like your context - or like you have in the ctor

NSGaga-mostly-inactive
  • 14,052
  • 3
  • 41
  • 51