I am working with a Windows Forms Application and have added Entity Framework 6 to the project. I have created a class where I can pass in the table name so I can use one class with many different tables. I want to use EF Code First without the wizard. How do you set the connection string for the DbContext? I know in ASP.NET MVC you can set it in the app.Config file but this is a Windows Forms Application. How do you set the connection string in order to get the data from the SQL Server?
public class TimerContext : DbContext
{
private readonly string _tableName;
public TimerContext(string tableName) : base("name=TimerContext")
{
_tableName = tableName;
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Timer>().ToTable(_tableName);
base.OnModelCreating(modelBuilder);
}
public IEnumerable<Timer> Timers { get; set; }
}
1 Answer 1
Right click on your project, choose Add -> New Items -> Application config file.
Add you connection string in the newly created app.config file and you're done.