实体框架和不同的环境(开发/生产) [英] Entity Framework and Different Environments (Dev/Production)

查看:84
本文介绍了实体框架和不同的环境(开发/生产)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近使用ASP.NET MVC进行了一个新项目,但我对该技术并不十分熟悉.我仍处于起步阶段,即将开始自学如何使用Entity Framework处理数据访问代码.在让软件/工具/功能可以做什么所起的作用时,我只是度过了艰难的时光,但是我认为我在使用EF的道路上是正确的.

I've recently undertaken a new project using ASP.NET MVC and I'm not terribly familiar with the technology. I'm still in the very early stages and am just about to start teaching myself how to use Entity Framework to deal with my data access code. I'm just having a rather tough time making heads or tails of what software/tool/feature can do what, but I think I'm on the right track with EF.

我正在寻找一种能够查看本地数据库与生产数据库中的差异(模式,而不是其中的实际数据)并将其发布到SQL Server中的东西.我们的本地网络.在我看来,它就像数据库的DVCS.

I'm looking for something that is going to be able to see the differences in my local database to that in my production database (schema, not the actual data in it) and be able to publish that to a SQL Server in our local network. In my mind, I think of it like a DVCS for databases.

传统上(就像我想说的那样),我会在代码或存储过程等中使用SQL编写自己的数据访问代码,而我自己会处理数据库方面的问题.不过,这对我来说不再是一个选择,因为我需要为开发和生产设置单独的环境,而且我真的没有时间也不想手动编写所有这些代码.开发人员将位于开发机器的本地,而生产将托管在我们本地网络上的服务器上.

Traditionally (like most I'd say) I would write my own data access code using SQL in my code or stored procedures etc. and I'd deal with the database side of things myself. That is no longer an option for me though because I need to set up separate environments for development and production, and I don't really have the time nor desire to manually write all of that code any more. Dev will be local to the dev machine and production will be hosted on a server on our local network.

我最近开始在Visual Studio中使用发布"功能,并且设法将其设置为可以很好地工作,将新版本发布到IIS服务器正在寻找的用于服务站点的目录中.

I've recently started using the "Publish" feature in Visual Studio and I have managed to set that up to work pretty well, publishing new versions to the directory which my IIS server is looking at to serve up the site.

我使用Git和Sourcetree处理我的项目代码的DVCS,但是我从未亲自使用或找到解决方案来对数据库进行类似的处理.起初,我以为我正在寻找某种用于SQL Server的DVCS,但这并没有带来太多回报.本质上,我只想要某种工具来管理本地数据库和生产数据库之间的版本差异,而我认为这是EF可以为我做的事情.

I use Git and Sourcetree to deal with DVCS for my project code, however I've never personally used or found a solution to do something similar for databasing. At first I thought I was looking for some kind of DVCS for SQL Server, but that didn't return much. Essentially I just want some kind of tool that is going to manage version differences between local and production databases, and I think that is something EF can do for me.

对于数据库,我正在使用SQL Server.我尚未为此项目建立数据库,因此我认为以代码为先的EF将是最好的方法.我想确保EF在SQL Server中使用数据库,但我相信可以通过指定要使用的连接字符串(而不是Visual Studio中的连接字符串)来进行设置.我偶尔需要通过SSMS进行数据库访问,以便可以使用索引和备份计划,并且通常可以通过SSMS进行访问,但是我仍然希望EF为我完成大部分工作.

For the databasing, I'm using SQL Server. I haven't set up a database yet for this project, so I'm thinking code-first EF would be the best approach. I want to ensure that EF is using a database in SQL Server though, which I believe I can set by specifying a connection string to use as opposed to one in Visual Studio. I need db access through SSMS occasionally so I can play with indexes and backup plans and such that I'd normally do through SSMS, but I'd still like EF to do the bulk of the work for me.

  1. 我可以使用实体框架实现我希望的目标吗?也就是说,是否拥有一个可以处理数据库差异和版本之间更新的工具? 我不想弄清楚哪些表/列等.我在本地进行了更改,必须以某种方式将其应用于生产数据库,但是我也不想更改生产db中的数据,只是结构...
  2. 我希望任何人都可以提供有关如何构建此设置的建议,甚至可以链接到有关如何进行设置的教程的链接.我确定我不是第一个想要这样做的人,而且我知道我不会成为最后一个.
  1. Can I achieve my desired goals using Entity Framework the way I'm hoping? That is, having a tool that deals with database differences and updates between versions? I don't want to have to figure out what tables/columns etc. I've changed locally and have to somehow work that into the production database, but I also don't want the data in the production db to change, just the structure...
  2. I'd love for anyone to provide advice on how this setup should be constructed, or even links to a tutorial on how to set it up. I'm sure I'm not the first person to want to do this, and I know I wont be the last.

基本上,我只想单击发布"按钮,并将代码和数据库中的所有本地差异都发布到生产服务器.

Essentially I'd just like to click the "Publish" button and have all local differences in both code AND database be published to the production server.

推荐答案

我可以使用Entity Framework达到自己的预期目标吗 希望吗?

Can I achieve my desired goals using Entity Framework the way I'm hoping?

这可以通过EntityFramework轻松完成.您正在寻找的被称为"EF迁移". EF迁移会扫描您的POCO类,以查看它们是否与您的数据库匹配.如果有新内容或更改过的内容,它将为您更新数据库表.要了解如何使用EF迁移,请查看此链接 https://msdn.microsoft. com/en-us/data/jj591621.aspx

That can be easily done by the EntityFramework. What you are looking for is called "EF Migrations". The EF Migrations scans your POCO classes to see if they match your database. If there is something new or changed, it will update the database tables for you. To learn how to use EF Migrations check out this link https://msdn.microsoft.com/en-us/data/jj591621.aspx

可以通过Web.Config/App.Config转换来解析连接字符串.在发布过程中,Web.Config转换会自动将开发连接字符串替换为生产连接字符串.若要查看如何使用web.config转换,请查看此链接

The connection string can be resolved by Web.Config/App.Config transformations. The Web.Config transformation automatically replaces the development connection string by the production connection string during the publishing. To see how to use web.config transformation, take a look at this link http://www.asp.net/mvc/overview/deployment/visual-studio-web-deployment/web-config-transformations

在发布期间,您可以告诉VisualStudio检查哪些迁移仍未应用到生产数据库,并自动应用这些迁移.本文

During the publishing, you can tell VisualStudio to check which migrations were still not applied to the production database, and apply those migrations, automatically. This article http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/migrations-and-deployment-with-the-entity-framework-in-an-asp-net-mvc-application shows how to apply migrations when publishing to Windows Azure.

希望有帮助!

这篇关于实体框架和不同的环境(开发/生产)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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