SqlException (0x80131904)
本文关键字:0x80131904 SqlException | 更新日期: 2023-09-27 18:01:09
我在stackoverflow上发现了类似的错误情况,但我认为我的问题不同,因为我找不到答案。
我在家里建了一个网站,并发布到一个网络主机上,他们也为我提供SQL Server。现在我可以访问SQL Server在本地机器上和发布后构建菜单,这样菜单就可以在我的网站运行时成功构建。所以,我知道连接字符串是有效的。
现在我已经创建了一个注册页面和一个登录页面,这两个页面都可以在我的本地机器上正常工作。然而,在发布到我的网络主机后,我试图从我的网站注册一个新用户,当我点击提交时收到了这个错误:
建立与SQL Server的连接时,发生了与网络相关或特定于实例的错误。找不到或无法访问服务器。请验证实例名称是否正确,以及SQL Server是否已配置为允许远程连接。(提供程序:SQL网络接口,错误:26-定位指定的服务器/实例时出错(
描述:在执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误以及错误在代码中的来源的更多信息。
异常详细信息:System.Data.SqlClient.SqlException:建立与SQL Server的连接时发生与网络相关或实例特定的错误。找不到或无法访问服务器。请验证实例名称是否正确,以及SQL Server是否已配置为允许远程连接。(提供程序:SQL网络接口,错误:26-定位指定的服务器/实例时出错(
来源错误:
在执行当前web请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常的起源和位置的信息。
以下是附加信息:
<connectionStrings>
<!--<add name="DefaultConnection" connectionString="Data Source=(LocalDb)'MSSQLLocalDB;AttachDbFilename=|DataDirectory|'aspnet-ffbpools-20160214093044.mdf;Initial Catalog=aspnet-ffbpools-20160214093044;Integrated Security=True" providerName="System.Data.SqlClient" />-->
<add name="connstring" connectionString="Data Source=10.23.136.135,780;uid=MyUserName;pwd=~FakePassword1;Initial Catalog=DBaccess_PNM" />
<add name="DBaccess_PNMEntities" connectionString="metadata=res://*/Models.SiteMenuEDModel.csdl|res://*/Models.SiteMenuEDModel.ssdl|res://*/Models.SiteMenuEDModel.msl;provider=System.Data.SqlClient;provider connection string="data source=10.23.136.135,780;initial catalog=DBaccess_PNM;persist security info=True;user id=MyUserName;password=~FakePassword1;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="DBaccess_PNMLoginDetails" connectionString="metadata=res://*/Models.LoginDetailsEDModel.csdl|res://*/Models.LoginDetailsEDModel.ssdl|res://*/Models.LoginDetailsEDModel.msl;provider=System.Data.SqlClient;provider connection string="data source=10.23.136.135,780;initial catalog=DBaccess_PNM;persist security info=True;user id=MyUserName;password=~FakePassword1;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
你提到了dbcontext,它更有意义,我做了更多的研究,发现我需要进行一些迁移,并遵循我尝试过的其他帖子的指示:PM>启用迁移-启用自动迁移
但我收到了以下信息:
More than one context type was found in the assembly 'ffbpools'.
To enable migrations for 'ffbpools.Models.ApplicationDbContext', use Enable-Migrations -ContextTypeName ffbpools.Models.ApplicationDbContext.
To enable migrations for 'ffbpools.Models.DBaccess_PNMLoginDetails', use Enable-Migrations -ContextTypeName ffbpools.Models.DBaccess_PNMLoginDetails.
To enable migrations for 'ffbpools.Models.DBaccess_PNMEntities', use Enable-Migrations -ContextTypeName ffbpools.Models.DBaccess_PNMEntities.
按照指示,我做了以下命令并得到了结果:
PM> Enable-Migrations -ContextTypeName ffbpools.Models.ApplicationDbContext
Checking if the context targets an existing database...
Code First Migrations enabled for project ffbpools.
我以为我在路上很顺利,直到我做到了:
PM> Enable-Migrations -ContextTypeName ffbpools.Models.DBaccess_PNMLoginDetails
Migrations have already been enabled in project 'ffbpools'. To overwrite the existing migrations configuration, use the -Force parameter.
我试过Force,得到了:
PM> Enable-Migrations -Force -ContextTypeName ffbpools.Models.DBaccess_PNMLoginDetails
Checking if the context targets an existing database...
System.NotSupportedException: Creating a DbModelBuilder or writing the EDMX from a DbContext created using Database First or Model First is not supported. EDMX can only be obtained from a Code First DbContext created without using an existing DbCompiledModel.
at System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext context, XmlWriter writer)
at System.Data.Entity.Utilities.DbContextExtensions.<>c__DisplayClass1.<GetModel>b__0(XmlWriter w)
at System.Data.Entity.Utilities.DbContextExtensions.GetModel(Action`1 writeXml)
at System.Data.Entity.Utilities.DbContextExtensions.GetModel(DbContext context)
at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, Boolean calledByCreateDatabase)
at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
at System.Data.Entity.Migrations.Design.MigrationScaffolder..ctor(DbMigrationsConfiguration migrationsConfiguration)
at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Run()
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldInitialCreate(String language, String rootNamespace)
at System.Data.Entity.Migrations.EnableMigrationsCommand.<>c__DisplayClass2.<.ctor>b__0()
at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
Creating a DbModelBuilder or writing the EDMX from a DbContext created using Database First or Model First is not supported. EDMX can only be obtained from a Code First DbContext created without using an existing DbCompiledModel.
进一步研究,我发现了以下链接:http://forums.asp.net/t/1975675.aspx?Enable+迁移+says+More+than+one+context+type+was+found+in+the+assembly+ScheduleWeb+似乎我可以''应该将DBaccess_PNMLoginDetails、DBaccess_PNMEntities和ApplicationDbContext组合成一个dbcontext不确定这是否正确ApplicationDbContext是我最初决定开始学习MVC时添加的东西。我理解如何使用这样简单的东西将DBaccess_PNMLoginDetails、DBaccess_PNMEntities组合到一个dbcontext中
public class MainDBContext : DbContext
{
public DbSet<DBaccess_PNMLoginDetails> DBaccess_PNMLoginDetails { get; set; }
public DbSet<DBaccess_PNMEntities > DBaccess_PNMEntities { get; set; }
}
但ApplicationDbContext不同,我认为这是因为我找到的代码。
public partial class DBaccess_PNMLoginDetails : DbContext
{
public DBaccess_PNMLoginDetails()
: base("name=DBaccess_PNMLoginDetails")
{
}
Bunch of stuff in here
}
而且。。
public partial class DBaccess_PNMEntities : DbContext
{
public DBaccess_PNMEntities()
: base("name=DBaccess_PNMEntities")
{
}
Bunch of stuff in here
}
ApplicationDbContext没有类似的内容,但三者都创建了一个相同的命名空间ffbpools。以下是我对ApplicationDbContext:的了解
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
我希望我解释得足够好。。。。。。。
有人能帮我解决这个问题吗?
这与我仍在学习和困惑有关,因为我会找到不同的答案,却没有意识到它们与我想要做的事情无关,也就是创建一个互联网网站。不是内部网,也不是代码优先。
无论如何,我最终结合了我的上下文文件,并开始向后工作,以验证我首先为DB和一个互联网网站编写代码。