SqlException(0x80131904)

时间:2016-08-07 21:23:40

标签: c# sql-server asp.net-mvc database-connection

我在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=&quot;data source=10.23.136.135,780;initial catalog=DBaccess_PNM;persist security info=True;user id=MyUserName;password=~FakePassword1;MultipleActiveResultSets=True;App=EntityFramework&quot;" 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=&quot;data source=10.23.136.135,780;initial catalog=DBaccess_PNM;persist security info=True;user id=MyUserName;password=~FakePassword1;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

你提到了更有意义的dbcontext,我做了更多的研究,发现我需要进行一些迁移,并按照我试过的其他帖子的指示: 的 PM&GT;启用 - 迁移-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中吗? 不确定这是否正确! 当我最初决定开始学习MVC时,我认为我首先添加了ApplicationDbContext。我理解如何将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();
        }
    }

我希望我解释得很好.......

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

这与我的学习和困惑有关,因为我会发现不同的答案,却没有意识到他们与我想做的事情无关,这就是创建一个互联网网站。不是内联网而不是代码优先。

无论如何,我最终合并了我的上下文文件并开始向后工作,以验证我是在为数据库编写代码还是为互联网网站编写代码。