实体框架代码优先 - 如何在开发完成后告诉我的应用程序现在使用生产数据库而不是创建本地数据库?

时间:2021-09-07 07:26:12

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.

我正在使用Code First和Entity Framework 5使用MVC4和MVC Scaffolding(链接/教程:http://www.codeproject.com/Articles/468777/Code-First-with-Entity-Framework-5-using-MVC4-和?msg = 4531106#xx4531106xx)从我的模型创建我的数据库。所以,基本上,当我运行应用程序时,如果模型更改,应用程序将丢弃并创建一个新数据库。那很棒。现在,显然,这对当地的发展很有帮助。

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....

但是当我的应用程序投入生产时呢?我如何告诉代码不再这样做,而是指出我的应用程序使用我在GoDaddy上托管的数据库?我将web.config中的连接字符串更改为指向GoDaddy数据库的连接字符串,但这不起作用。会发生什么是代码仍然尝试使用旧的连接字符串由于我不知道的某些原因。我似乎无法让我的应用程序现在使用我在GoDaddy上创建的数据库。我开始后悔走下代码优先路线....

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

这是我的Application_Start方法中自动生成数据库的代码行(在这种情况下,仅当我的模型更改时):

System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges<CapWorx.QuikCap.Models.CapWorxQuikCapContext>());

2 个解决方案

#1


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.

如果你有'非静态ctor'CapWorxQuikCapContext():base(“connection”) - 那么你也必须在配置中使用相同的名称。

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

否则 - 您必须将连接字符串命名为与Context类相同 - 即恰好是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

如果你不这样做 - EF / CF建立自己的“连接”路径 - 即制作一些默认的数据库名称 - 就像你的上下文一样 - 或者就像你在ctor中那样

#2


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:

请注意,此时CodeFirstSeeder是无关紧要的 - 它只是我安装的一种策略,用于播种一些示例数据。无论如何,我改变了以上内容:

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

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

这告诉我的应用程序查看名为“DefaultConnection”的连接字符串并将其用作其数据源。

#1


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.

如果你有'非静态ctor'CapWorxQuikCapContext():base(“connection”) - 那么你也必须在配置中使用相同的名称。

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

否则 - 您必须将连接字符串命名为与Context类相同 - 即恰好是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

如果你不这样做 - EF / CF建立自己的“连接”路径 - 即制作一些默认的数据库名称 - 就像你的上下文一样 - 或者就像你在ctor中那样

#2


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:

请注意,此时CodeFirstSeeder是无关紧要的 - 它只是我安装的一种策略,用于播种一些示例数据。无论如何,我改变了以上内容:

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

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

这告诉我的应用程序查看名为“DefaultConnection”的连接字符串并将其用作其数据源。