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; }
}
I don’t understand your question. By default you should already have <connectionStrings> on your web.config pro
MSSQLLocalDB
. What else do you need?– George Wurthmann
I don’t have the string, I wanted to know how to create it and how to aim in my Dbcontext!
– Luhhh