winformsentity-frameworkef-code-first

How to make a new database using Code First in WinForms


I want to make a MSSQL using code-first approach. I have defined two models - Product and Category and then defined the DbContext as you can see below:

public class ApplicationDbContext : DbContext
{
    public DbSet<Product> Products { get; set; }
    public DbSet<Category> Categories { get; set; }


    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
    }
}

But I have not managed to find how and where to put the connection string and update the database after creating a migration.


Solution

  • Well, I just had to override the OnConfiguring method and add the connection string there.

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("Connection String...");
    }