【发布时间】:2015-11-04 12:24:53
【问题描述】:
在连接到我们拥有的 MS SQL 数据库服务器时,我的应用程序似乎忽略了数据库连接字符串,而且我在 ASP.Net 或 IIS 方面没有经验,所以我不确定错误是什么或在哪里。
作为背景,我在 .Net 4.5 上有一个工作的 ASP.Net MVC 应用程序,不幸的是我需要将它部署在只运行 .Net 4 的旧服务器上。因此我不得不将它回滚到 .Net 4 ,这需要一段时间,我需要更换 NuGet 包并删除任何报告兼容性错误的部分。
当我从 VS Community 2015 本地运行应用程序时,它工作正常,连接到数据库,加载所需内容。当我将它发布到我们的测试服务器(使用 IIS 7 并运行 .Net 4 应用程序池)时,它会出现以下错误:
用户“”登录失败
有一个例外:
[InvalidOperationException: This operation requires a connection to the 'master' database. Unable to create a connection to the 'master' database because the original database connection has been opened and credentials have been removed from the connection string. Supply an unopened connection.]
我用谷歌搜索了异常并尝试了添加的解决方案:
Database.SetInitializer<FormsContext>(null);
到 application_start 方法。然后导致了这个异常(与用户''相同的错误):
[EntityException: the underlying provider failed to open]
我不确定这是好是坏。我的连接字符串(密码和服务器空白)是:
<add name="DefaultConnection" connectionString="Data Source=<server>;Initial Catalog=forms;Integrated Security=False;Trusted_Connection=False;Persist Security Info=True;User ID=formsReadWrite;Password=<password>;MultipleActiveResultSets=True;Encrypt=True;TrustServerCertificate=True;Application Name=EntityFramework" providerName="System.Data.SqlClient" />
<add name="FormsContext1" connectionString="Data Source=<server>;Initial Catalog=forms;Integrated Security=False;Trusted_Connection=False;Persist Security Info=True;User ID=formsReadWrite;Password=<password>;MultipleActiveResultSets=True;Encrypt=True;TrustServerCertificate=True;Application Name=EntityFramework" providerName="System.Data.SqlClient" />
我现在有点不知所措,因为它以前可以工作,而且似乎可以在我的本地机器上工作。我到处搜索过,但大多数答案都涉及使用集成安全性,我绝对不会这样做。任何帮助将不胜感激。
连接数据库的代码是(db上下文的开始,在此之后相当大,并且只是创建每个字段):
public partial class FormsContext : DbContext
{
public FormsContext()
: base("name=FormsContext1")
{
}
public virtual DbSet<form> forms { get; set; }
public virtual DbSet<input> inputs { get; set; }
public virtual DbSet<option> options { get; set; }
public virtual DbSet<section> sections { get; set; }
public virtual DbSet<user> users { get; set; }
public virtual DbSet<reveal> reveals { get; set; }
public virtual DbSet<survey> surveys { get; set; }
连接代码:
using (var data = new FormsContext())
{
//Query the database for the form details
var detailsQuery = (from d in data.forms
where d.name == name
select d);
var details = new forms.form();
它连接了几次,但使用与上面相同的逻辑。
【问题讨论】:
-
在您的测试服务器上,您是否授予用户
formsReadWrite对sql server 的访问权限? -
是的,它与 .net 4.5 应用程序使用的连接字符串相同,当我使用相同的应用程序池将其发布到同一站点、同一服务器上时,它工作正常。这样用户肯定可以访问 SQL 服务器。
-
你能把你提供给实体框架的连接字符串的代码贴出来吗?您能否确保测试服务器上的 web.config 具有与调试环境中相同的内容?所以我们可以排除尽可能多的可能性。
-
我检查了开发服务器,web.config 肯定是一样的。代码现在在问题中,它创建了一个 dbcontext(我没有把所有的都放进去,因为它相当大),并且连接使用它。
-
也许我在这里遗漏了一些东西,但是您是否尝试将
base("name=FormsContext1")更改为base("FormsContext1")?
标签: c# asp.net .net asp.net-mvc entity-framework