[英]SqlException (0x80131904)
我在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> Enable-Migrations -EnableAutomaticMigrations
但是我收到以下消息:
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+Migrations+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();
}
}
我希望我解释得足够好……。
有人可以帮我解决这个问题吗?
这与我仍在学习和感到困惑有关,因为我会发现不同的答案而没有意识到它们与我想要做的事情无关,那就是创建一个互联网站点。 不是Intranet,也不是首先编码。
无论如何,我最终合并了上下文文件,并开始向后工作以验证我是先为DB还是为互联网网站编写代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.