ASP.NET MVC-如何首先使用实体​​框架模型/数据库部署到Azure? [英] ASP.NET MVC - How to Deploy to Azure using Entity Framework Model/Database First?

查看:77
本文介绍了ASP.NET MVC-如何首先使用实体​​框架模型/数据库部署到Azure?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我首先使用Entity Framework数据库构建了我的应用程序。我的数据库只有2个表,非常小。

So, i built my application using Entity Framework database first. My database only has 2 tables, it's very small.

我在YT上看过一些有关部署到Azure的视频,但看起来它们都使用CodeFirst方法,然后使用在Package Manager上启用迁移,然后他们使用复选框发布应用程序以执行代码迁移。

I've seen some videos on YT about deploying to Azure but looks like all of them use CodeFirst approach, then use enable migration on Package Manager, and then they publish the application using a checkbox to execute code migrations.

但是由于我使用Database First构建了应用程序,所以我无法使用这项特征。启用迁移功能仅适用于Code First。

But since i built my application using Database First, i can't use this feature. The enable-migration feature only works for Code First.

那么,我该怎么做才能将数据库放在Azure上并部署应用程序?

So, what can i do to put my database on the Azure and deploy my application ?

使用解决方案进行编辑:

EDIT WITH SOLUTION:

在SQL Server管理工具上,如果单击数据库上的右键,然后转到任务,则表示'将找到以下选项在Azure SQL服务器数据库上植入数据库。

On SQL Server Management Tool, if you click with the Right Button on your Database and go to Task, you'll find the following option "Implant database on Azure SQL server Database".

您只需指定云中服务器的实例,登录名和密码,然后单击下一步,它将为您部署数据库。
重要:首先,您需要与本地计算机和Azure Sql Server建立连接。为此,您需要将计算机的IP地址插入云中服务器的防火墙定义中。在此链接中对此进行了很好的解释: https://docs.microsoft.com/zh-cn/aspnet/mvc/overview/getting-started/database-first-development/publish-to-azure

You just need to specify the instance of the server in the cloud, the login and password, and click next and it'll deploy the database for you. IMPORTANT: FIRST, you need to make a connection with your local machine and the Azure Sql Server. To do that, you need to insert the IP Adress of your machine in the firewall definitions of the server on the cloud. It's well explained in this link: https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/database-first-development/publish-to-azure

然后,在迁移数据库后,您只需要获取在AzureCloud上插入的新数据库的ConnectionString,然后转到Visual Studio上的应用程序,发布,更新您的连接字符串并部署。

Then, after you migrate the database, you just need to get the ConnectionString of the new database that you inserted on the AzureCloud, and then you go to your application on Visual Studio, Publish, update your connection string and deploy.

推荐答案

按照以下步骤操作,您将很容易:

Follow these steps you will be good to go:


  1. 右键单击项目,然后选择发布。

  1. Right-click the project and select Publish.

选择Microsoft Azure网站。

Select Microsoft Azure Websites.

选择新建以创建一个新的Web应用程序。(如果未登录,请输入您的凭据)

Select New to create a new web app.(If not login, Enter your credentials)

填写必要的详细信息为数据库创建新服务器,并为此新数据库服务器提供用户名和密码。完成后,单击创建。

Fill the necessary details and Create new server for the database, and provide a user name and password for this new database server. When finished, click Create.

保留不变的值,这些都是您的连接值。

Leave values unchanged, those are your connections values.

单击数据库连接旁边的省略号(...),记下数据库服务器和数据库的名称。

Click ellipsis (...) next to the database connection, Note the name of the database server and the database.

单击确定,然后在发布中在Web窗口中,单击下一步以查看预览。单击发布。

Click OK and in the Publish Web window, click Next to see the preview. Click Publish.

发布后,该网站将立即在网络浏览器中启动。您的站点已部署,但是,由于表尚未发布,因此您将收到错误消息。

After publication, the site is immediately launched in a web browser. Your site has been deployed, however, you will receive an error since your table has not been published yet.

将数据库发布到SQL Azure


  • 在发布数据库之前,必须确保本地计算机可以连接到数据库服务器。数据库服务器的防火墙限制了哪些计算机可以连接到数据库。您需要将计算机的IP地址添加到允许的防火墙IP地址中。

  • 通过Azure门户登录到Azure帐户。

  • 选择新数据库,然后选择管理。

  • 您必须配置数据库服务器以允许来自计算机的连接。选择管理时,要求您将允许的当前IP地址添加到数据库服务器。选择是。

  • 在上一步中添加的IP地址可能不是唯一需要配置用于连接的IP地址。您可以尝试登录数据库以查看连接是否已正确设置。提供您之前创建的用户和密码。

  • Before publishing your database, you must make sure your local computer can connect to the database server. The firewall for your database server restricts which machines can connect to the database. You need to add the IP address of your computer to the allowed IP addresses for the firewall.
  • Login to your Azure account through the Azure portal.
  • Select your new database and select Manage.
  • You must configure your database server to allow connections from your computer. When you select Manage, you are asked to add the current IP address as permitted to the database server. Select Yes.
  • There is a chance that the IP address you added in the previous step is not the only IP address you need to configure for connections. You can attempt to login to the database to see if the connections have been properly set up. Provide the user and password you created earlier.

如果收到错误消息,则需要添加另一个IP地址。单击错误消息以查看有关错误的更多详细信息。在详细信息中,您将看到需要添加的IP地址。记下该IP地址。


  • 关闭此登录窗口,然后返回到Azure门户。导航到数据库的仪表板。单击管理允许的IP地址。

  • Close this login window, and return to the Azure portal. Navigate to the Dashboard for your database. Click Manage allowed IP addresses.

您现在必须从错误消息中添加IP地址。更改允许的IP地址范围以包括错误消息中的一个,或者将该IP地址添加为单独的条目。

You must now add the IP address from the error message. Either change the range of allowed IP addresses to include the one from the error message or add that IP address as a separate entry.

将更改保存到允许的IP地址。单击管理,然后尝试再次登录到数据库。您可能需要等待几分钟,然后才能为防火墙正确配置允许的IP地址。当您可以成功登录数据库后,就完成了与数据库的连接。

Save the change to allowed IP addresses. Click Manage, and try logging in again to the database. You may need to wait a few minutes before the allowed IP addresses are correctly configured for the firewall. When you can successfully log in the database, you have finished setting up your connection to the database.

您可以将此管理窗口保持打开状态,因为您将很快检查数据库部署的结果。


  • 返回到您的数据库项目。右键单击该项目,然后选择发布。

  • Return to your database project. Right-click the project and select Publish.

在发布数据库窗口中,选择编辑。

In the Publish Database window, select Edit.

提供数据库服务器的名称以及该服务器的身份验证凭据。提供凭据后,从可用数据库列表中选择您创建的数据库。默认情况下,Visual Studio将数据库字段的名称设置为项目的名称,该名称可能与您创建的数据库不同。单击确定。单击发布。

Provide the name of the database server and your authentication credentials for the server. After providing the credentials, select the database you created from the list of available databases. By default, Visual Studio sets the name of the database field to the name of your project which might not be the same as the database you created.Click Ok. Click Publish.

您已完成

< a href = https://docs.microsoft.com/zh-cn/aspnet/mvc/overview/getting-started/database-first-development/publish-to-azure rel = nofollow noreferrer> docs.microsoft .com的文档很好

这篇关于ASP.NET MVC-如何首先使用实体​​框架模型/数据库部署到Azure?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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