Tiedt Tech
Tiedt Tech

Reputation: 717

Error when trying to connect to the database EF and SQL Server

I'm doing test with a new host, and I am facing a problem with the connection to the database.

I already have the same project going on another hosting and running on SQL Server 2005.

But this new hosting, SQL Server is 2012 and when trying to get a page that fetches data from the database returns this error:

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)

Connection string:

<connectionStrings>
    <add name="ERPContext" 
         connectionString="Data Source=SQL5003; Initial Catalog=DB_99C4E9;User Id=DB_99C4E9;Password=senha;" 
         providerName="System.Data.SqlClient" />
</connectionStrings>

ERPContext:

    public class ERPContextUser : DbContext
    {
        public DbSet<UsuarioAtivacao> UsuarioAtivacao { get; set; }

        public ERPContextUser() : base("ConexaoERP")
        {
            Database.SetInitializer<ERPContextUser>(null);
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<UsuarioAtivacao>().ToTable("Usuario");
        }
    }

    public class ERPContext : DbContext
    {
        public DbSet<Empresa> Empresa { get; set; }
        public DbSet<Pessoa> Pessoa { get; set; }
        public DbSet<Usuario> Usuario { get; set; }
        public DbSet<UsuarioAcesso> UsuarioAcesso { get; set; }
        public DbSet<SimNao> SimNao { get; set; }        
        public DbSet<Sexo> Sexo { get; set; }
        public DbSet<TipoPessoa> TipoPessoa { get; set; }
        public DbSet<UnidadeMedida> UnidadeMedida { get; set; }

        public ERPContext()
            : base("ConexaoERP")
        {
            Database.SetInitializer<ERPContext>(null);
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Empresa>().ToTable("Empresa");
            modelBuilder.Entity<Pessoa>().ToTable("Pessoa");            
            modelBuilder.Entity<Usuario>().ToTable("Usuario");
            modelBuilder.Entity<UsuarioAcesso>().ToTable("UsuarioAcesso");
        }
    }

Upvotes: 0

Views: 136

Answers (1)

cuongle
cuongle

Reputation: 75306

seem you need to change the name ERPContext to ConexaoERP in your web.config

Upvotes: 1

Related Questions