Connection to the database in ASP.NET with Entityframework

Asked

Viewed 977 times

2

I need to migrate a system from one server to another. The system is in ASP.NET and the connection to the database is a little strange. I need to know how to connect to the database.

The only thing I found pertaining to Connection in the Web.Config was: http://prntscr.com/brnfqs.

What I do?

  • 1

    What do you mean "it’s weird"? What’s happening? Click [Edit] and give more details...

  • This is not a Connection string. This is the EF configuration. A Connection string is among <connectionStrings></connectionStrings>.

  • So, man, how do I connect to the database with the Entityframework configuration on ASP.NET?

2 answers

1

Dude, on the Web.Config is gonna have something like this:

  <connectionStrings>
    <add name="ProjetoContext" providerName="System.Data.SqlClient" connectionString="Server=IP/ENDEREÇO_DO_SERVER;Database=NOME_DO_BD;Trusted_Connection=false;Persist Security Info=True;User ID=NOME_USUARIO;Password=SENHA;MultipleActiveResultSets=True" />
    </connectionStrings>

Only modify according to your new server and database settings(IP, database name, user, password).

  • <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.Sqlconnectionfactory, Entityframework" /> <providers> <Provider invariantName="System.Data.Sqlclient" type="System.Data.Entity.SqlServer.Sqlproviderservices, Entityframework.Sqlserver" /> </providers> </entityFramework>

1


In the Entity Framework you need to tell the context what the database connection is, this is done by moving to the base class : base("DataContext") your connection name as shown below.

public class ProjetoModeloContext : DbContext
{
    public ProjetoModeloContext()
        : base("DataContext")
    {

    }
    public DbSet<Categories> Categories { get; set; }
}

Configuration of connectionStrings on the web config, just below the </appSettings>

</appSettings>
<connectionStrings>
<add name="DataContext" connectionString="Server=SEU_SERVIDOR; database=NORTHWND;User ID=SEU_USUARIO;Password=SUA_SENHA" providerName="System.Data.SqlClient" />
</connectionStrings>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.