简体   繁体   中英

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");
        }
    }

似乎您需要在web.config中将名称ERPContext更改为ConexaoERP

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