简体   繁体   English

MVC C#从本地主机迁移到生产

[英]MVC C# migration from localhost to production

I just deployed a web application from local host with SQL express (not IIS local host) to production. 我只是将Web应用程序从具有SQL Express的本地主机(不是IIS本地主机)部署到生产中。

The web application has controllers, models, views (MVC). 该Web应用程序具有控制器,模型,视图(MVC)。 I made some modifications to the database tables. 我对数据库表做了一些修改。 Each time I added a new field I: 每次我添加一个新字段时,我:

enable-migrations
add-migration addnewfieldname
update-database -verbose

All the models get updated with the new table field. 所有模型都会使用新的表格字段进行更新。 The above worked well with my web application on the local host with SQL express. 以上与SQL Express在本地主机上的Web应用程序配合良好。

So then I published it to production with all the migrations. 因此,我将所有迁移都发布到了产品中。 also on production I alter the tables on the database in SQL server to have all the newly created fields. 同样在生产中,我更改了SQL Server中数据库中的表,使其具有所有新创建的字段。

Now comes the problem, production is not working. 现在出现了问题,生产无法正常工作。 what did I do wrong? 我做错了什么? It gives me error page. 它给我错误页面。

Here is error message: 这是错误消息:

The model backing the 'DefaultConnection' context has changed since the database was created. 自创建数据库以来,支持“ DefaultConnection”上下文的模型已更改。 Consider using Code First Migrations to update the database 考虑使用代码优先迁移来更新数据库

Update 更新资料

So I found the solution, this is amazing! 所以我找到了解决方案,这真是太神奇了!

First, make sure there are no changes to the UserProfile class. 首先,确保没有对UserProfile类进行任何更改。 Make sure you are connected to the production db first in your web.config then run: 确保首先在web.config中连接到生产数据库,然后运行:

Enable-Migrations
Add-Migration InitialMigrations -IgnoreChanges
**this below here is key, you need to add the new field one at a time.**
add-migration addnewfield1
update-database

add-migration addnewfield2
update-database


add-migration addnewfield3
update-database

etc.

This should generate a blank "InitialMigration" file. 这将生成一个空白的“ InitialMigration”文件。 Now, add any desired changes to the UserProfile class. 现在,将任何所需的更改添加到UserProfile类。 Once changes are added, run the update command again: 添加更改后,再次运行update命令:

update-database -verbose

Now the automatic migration will be applied and the table will be altered with your changes. 现在,将应用自动迁移,并且您的更改将更改表。

Another key, if you don't have permissions on SQL Server to alter tables, make sure you send the scripts to your sysadmin as it won't alter the tables. 另一个关键,如果您没有SQL Server更改表的权限,请确保将脚本发送给sysadmin,因为它不会更改表。 This is after you have done all the above. 完成以上所有操作之后。

The error means that when you updated database manually, you made some mistakes, so the model in your database does not reflect your entites. 该错误意味着在手动更新数据库时,您犯了一些错误,因此数据库中的模型无法反映您的实体。

  1. I used to change connection string in my web.config file to to my production database and run "Update-Database", if database you are working on is already populated with some data that you don't want to lose then better just copy tables (including Migrations table) from your local sql. 我曾经将web.config文件中的连接字符串更改为生产数据库并运行“ Update-Database”,如果您正在使用的数据库已经填充了一些您不想丢失的数据,那么最好只是复制表(包括“迁移”表)从您的本地sql。

  2. It's bad idea to update tables on production manually, because you might make many mistakes and EF won't work then. 手动更新生产中的表是个坏主意,因为您可能会犯很多错误,并且EF无法使用。

Best way to fix this is to delete all tables from database (if you can afford it), change your connection string from local to production (in web.config) and run "Update-Database" command from Visual Studio. 解决此问题的最佳方法是从数据库中删除所有表(如果可以承受的话),将连接字符串从本地更改为生产(在web.config中),然后从Visual Studio运行“ Update-Database”命令。

If you cannot drop database because it's already populated with some data, copy manually all tables from local that Entity Framework has created including migrations table, then populate migrations table with data from local. 如果由于已经填充了某些数据而无法删除数据库,请手动复制Entity Framework已创建的本地所有表(包括迁移表),然后使用本地数据填充迁移表。 That might help but I can't guarantee it. 这可能会有所帮助,但我不能保证。

It looks like your schema of db have been changed. 看来您的db模式已更改。

Had the same issue. 有同样的问题。 But I have the production server which have to work 24/7. 但是我有必须24/7全天候工作的生产服务器。 For situation like that I use: 对于这种情况,我使用:

http://www.devart.com/dbforge/sql/datacompare/ http://www.devart.com/dbforge/sql/datacompare/

this will help you to compare the data in your local db and production server db. 这将帮助您比较本地数据库和生产服务器数据库中的数据。 But if you need to compare the schema use this one: 但是,如果您需要比较架构,请使用以下代码:

http://www.devart.com/dbforge/sql/schemacompare/ http://www.devart.com/dbforge/sql/schemacompare/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM