简体   繁体   中英

SqlException (0x80131904)

I found similar situations for this error on stackoverflow, however, I think my problem is different since I couldn't find an answer.

I have a website I build at home and publish to a web host where they also supply SQL Server for me. Right now I can access SQL Server to build my menu both on my local machine and after publishing so the menu successfully gets built when my website is live. So, I know the connection string works.

Now I have created a Registration page as well as a Login page and both work fine on my local machine. However, after publishing to my web host I try to register a new user from my website and get this error when I hit submit:

A network-related or instance-specific error occurred while establishing a connection to 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. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to 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. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Here is the additional information:

  <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>

And you mentioned the dbcontext which made even more sense and I did some more researching and found that I needed to do some migrations and following the directions from other posts I tried: PM> Enable-Migrations -EnableAutomaticMigrations

But I got the below message:

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.

Following the directions I did the following command and got the results:

PM> Enable-Migrations -ContextTypeName ffbpools.Models.ApplicationDbContext
Checking if the context targets an existing database...
Code First Migrations enabled for project ffbpools.

I thought I was well on my way until I did:

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.

I tried Force and got:

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.

Looking further into this I found this link: http://forums.asp.net/t/1975675.aspx?Enable+Migrations+says+More+than+one+context+type+was+found+in+the+assembly+ScheduleWeb+ and it appears I can\\should combine DBaccess_PNMLoginDetails, DBaccess_PNMEntities and ApplicationDbContext into one dbcontext? Not sure if this is correct! ApplicationDbContext is something I believe I added at first when I originally decided to start learning MVC. I understand how combining DBaccess_PNMLoginDetails, DBaccess_PNMEntities into one dbcontext is possible using something as easy as:

public class MainDBContext : DbContext

    {
            public DbSet<DBaccess_PNMLoginDetails> DBaccess_PNMLoginDetails { get; set; }
            public DbSet<DBaccess_PNMEntities > DBaccess_PNMEntities  { get; set; }
    }

but ApplicationDbContext is different and and the reason I think this is because of the code I found.

public partial class DBaccess_PNMLoginDetails : DbContext
{
    public DBaccess_PNMLoginDetails()
        : base("name=DBaccess_PNMLoginDetails")
    {
    }

Bunch of stuff in here
}

And..

  public partial class DBaccess_PNMEntities : DbContext
    {
 public DBaccess_PNMEntities()
            : base("name=DBaccess_PNMEntities")
        {
        }

    Bunch of stuff in here
    }

ApplicationDbContext doesn't have anything like this, but all three created a the same namespace of ffbpools . Here is what I have for the ApplicationDbContext:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }

I hope I explained it well enough.......

Can someone please help me solve this?

This had to do with me still learning and getting confused as I would find different answers without realizing they didn't pertain to what I want to do, which is create an internet site. Not intranet and not Code first.

Anyway, I eventually combined my context files and started working backwards to verify I am writing code for DB first and for an internet website.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM