实体框架代码第一 - 如何告诉我的应用程序现在使用生产数据库一旦开发完成,而不是创建本地数据库? [英] Entity Framework Code First - How do I tell my app to NOW use the production database once development is complete instead of creating a local db?

查看:141
本文介绍了实体框架代码第一 - 如何告诉我的应用程序现在使用生产数据库一旦开发完成,而不是创建本地数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用代码优先与实体框架5使用MVC4和MVC脚手架(链接/教程: http://www.codeproject.com/Articles/468777/Code-First-with-Entity-Framework-5-using- MVC4和?msg = 4531106#xx4531106xx )从我的模型创建我的数据库。所以,基本上,当我运行的应用程序,该应用程序下降并创建一个新的数据库,如果我的模型改变。那很棒。现在,显然,这是伟大的地方发展。



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



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

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


解决方案

(根据我们的讨论/ >

简单的规则是 -



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



否则,您必须将连接字符串命名为与上下文类相同 - 即完全是CapWorxQuikCapContext。



如果没有 - EF / CF创建自己的连接路径 - 即创建一个默认的数据库名称


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>());

解决方案

(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

这篇关于实体框架代码第一 - 如何告诉我的应用程序现在使用生产数据库一旦开发完成,而不是创建本地数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆