简体   繁体   English

Database First DBContext在本地工作,但在部署时无法工作

[英]Database First DBContext works locally but not when deployed

I deployed a webapi project to azure that connects to a database via a DbContext. 我部署了一个webapi项目,以确保通过DbContext连接到数据库。 Everything works locally but when I deploy I am hit with an error anytime I make a request that will need to access my DbContext. 一切都在本地工作,但是当我部署时,只要我提出需要访问我的DbContext的请求,就会遇到错误。

I open a connection as follows: 我按如下所示打开连接:

 public ProductRepo(ProdStoreEntities context)
        {
            dbContext = context;
            dbContext.Database.Connection.Open();
        }

My web.config is as follows: 我的web.config如下:

<connectionStrings>
   <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-CatalogAPI-20160227031830.mdf;Initial Catalog=aspnet-CatalogAPI-20160227031830;Integrated Security=True" providerName="System.Data.SqlClient" />
    <add name="ProdStoreEntities" connectionString="metadata=res://*/DbCache.csdl|res://*/DbCache.ssdl|res://*/DbCache.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\SQLEXPRESS;initial catalog=ProdStore;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

I would have thought that everything would be handled for me since I am using a DbContext but when I deploy and attempt to make a get request that would require a database query I recieve the following error: 我以为自从我使用DbContext以来,一切都会为我处理,但是当我部署并尝试发出需要数据库查询的get请求时,我收到以下错误:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. 建立与SQL Server的连接时发生与网络相关或特定于实例的错误。 The server was not found or was not accessible. 服务器未找到或无法访问。 Verify that the instance name is correct and that SQL Server is configured to allow remote connections. 验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) (提供者:SQL网络接口,错误:26-指定服务器/实例时出错)

Any ideas on why this might be happening? 为什么会发生这种情况的任何想法? I haven't done anything special when publishing, I literally right click my class libray and hit publish. 发布时我没有做任何特别的事情,实际上我右键单击类libray并单击“发布”。 I initally generated the DbContext via a database first approach if that matters. 如果重要的话,我通过数据库优先方法最初生成了DbContext。

This could be from a few things. 这可能来自几件事。

Check that you are running Localdb on your deployment server 检查您是否在部署服务器上运行Localdb

Secondly, check that the database has been intialized on the deployed server. 其次,检查数据库是否已在已部署的服务器上初始化。

Thirdly, are you able to to login to the database via management studio via the credentials you're providing - Make sure your user details has the privileges on the deployed server to access the database 第三,您是否能够使用提供的凭据通过Management Studio登录数据库-确保您的用户详细信息在已部署的服务器上具有访问数据库的特权

Your problem is that your connection string says that it is a local database. 您的问题是您的连接字符串说这是一个本地数据库。 what you have to do is create a database in Azure, when you have done that you can actually see a connection string to that database, that connection string needs to replace your current connection string where you have your (local) db 您要做的是在Azure中创建一个数据库,当您完成操作后就可以实际看到该数据库的连接字符串,该连接字符串需要替换您当前拥有(本地)数据库的连接字符串。

暂无
暂无

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

相关问题 JavaScript在本地运行,但在部署到Web服务器时无法运行 - JavaScript works locally but not when deployed to webserver 简单的WebClient在本地工作,但在部署到Azure时无法工作 - Simple WebClient works locally, but not when deployed to Azure 当“第一次”DbContext访问数据库时到底是什么时候? - When exactly is the “first time” a DbContext accesses the database? 我的asmx Web服务在本地工作,但在远程IIS 7上部署时不工作 - My asmx webservice works locally but not when deployed on remote IIS 7 具有约束的属性路由在本地工作,但在部署时会失败 - Attribute Route with constraint works locally but fails when deployed ASP.NET Core(HttpSys)路由在本地工作,但在部署时不工作 - ASP.NET Core (HttpSys) Routing works locally but not when deployed 将映像上传到Azure Blob存储-本地工作,部署后失败 - Uplod images to azure blob storage - Works locally, fails when deployed Google Map Direction Api在本地工作正常,但在服务器上部署后无法正常工作 - Google Map Direction Api Works Fine Locally but does not work properly when deployed on Server C# Azure ServiceBus / Azure SQL - 在本地工作,部署到 Azure 时似乎不起作用 - C# Azure ServiceBus / Azure SQL - works locally, does not seem to work when deployed to Azure Angular 8 - 路由在本地工作,但在生产环境中部署时,路由不起作用 - .NET 核心 C# 后端 - Angular 8 - routing works locally, but when deployed on Production, routes don't work - .NET Core C# backend
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM