Entity Framework Connection String

Asked

Viewed 6,800 times

2

I am creating a project using Entity Framework.

My question would be, how can I create a connectionString (for SQL Server for example) and set in my Dbcontext where it will get from the Web.config file?

Dbcontext

namespace Data
{
    public class DBContext : DbContext
    {
        public DBContext() : base("dbName")
        {

        }

        public DbSet<Usuarios> Clientes { get; set; }
        public DbSet<Vendedores> Produtos { get; set; }

    }
}
  • I don’t understand your question. By default you should already have <connectionStrings> on your web.config pro MSSQLLocalDB. What else do you need?

  • I don’t have the string, I wanted to know how to create it and how to aim in my Dbcontext!

1 answer

5


Your connectionStrings should look like this on the web.config:

To create in Localdb:

<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Nome_Arquivo.mdf;Initial Catalog=Nome_Arquivo;Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>

To create in SQL Server (if installed):

  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Nome_BD;Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>

The first connectionStrings string is automatically created on your web.config when you start a MVC project with EF. Check that it no longer exists before you create it, and if you wish, change to the second example.

You do not need to point in your Dbcontext. If you want the database to be already created (Codefirst), activate Migrations on Package Manager Console typing:

Enable-Migrations

After that the directory will appear Migrations in your project. Open the file Configuration.cs in that directory and activate the options below:

public Configuration()
    {
        AutomaticMigrationsEnabled = true;
        AutomaticMigrationDataLossAllowed = true;
    }

With these settings you can now use the command below on Package Manager Console:

Update-Database

This command will create your Database. The MDF file will appear in the folder App-Data (if you created a Localdb) and if you click Show all files can open it to view its database on Server-Explore.

If you created in SQL Server, just open Management Studio to see the database.

Your Dbcontext should look like this (already with the 2 Dbset of your question):

public class DBContext : DbContext
    {
        public DBContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }

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

        public DbSet<Usuarios> Clientes { get; set; }
        public DbSet<Vendedores> Produtos { get; set; }
    }
  • Thanks for the help. And in dbcontext, I don’t need to enter the database name?

  • I edited the answer, but I think it should already be like this. No need to make any other notes. web.config is in charge of indicating the connection.

  • Thank you, to finish, which changes Identitydbcontext instead of Dbcontext

  • It is a class that uses standard Identity types. Maybe you are not using in your project. If you create a standard VS example, it automatically generates if you use Identity.

  • I edited it to look like the Dbcontext of your question. I hadn’t noticed you weren’t using Identity.

  • Thanks. I noticed that this string is for connection to Appdata database, and if it was to a local SQL Server database directly?

  • I put the other string in the answer too.

Show 2 more comments

Browser other questions tagged

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