Creating mysql database in my winforms app with EF

44 views Asked by At

I am trying to create my first winforms with EF application.

I have created a model and now I want to create the database and table.

I have 3 projects, the main project, the main.data project and the main.data.update project. We use a similair structure for web apps.

In the main.data project I have a folder called "Enitities" and in that folder I have the class with all the properties for each colum I want in the database table.

public class CustomerData
{
  public int? CustomerDataId { get; set; }
  public string? KlantNaam { get; set; }
  public int? SoortConnectie { get; set; }
  public string? DNS { get; set; }
  public string? DataSource { get; set; }
  public string? ClientId { get; set; }
  public string? TenantId { get; set; }
  public string? ClientSecret { get; set; }
  public bool? Tls { get; set; }
  public bool? Tls11 { get; set; }
  public bool? Tls12 { get; set; }
}

I have this for the dbcontext

public class AnvaTestDbContext : DbContext 
{
  public DbSet<CustomerData> CustomerData { get; set; }
  
  public AnvaTestDbContext() { }

  protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  {
    if (!optionsBuilder.IsConfigured)
    {
      var connectionData = (ConnectionData)ConfigurationManager.GetSection("ConnectionStrings");

      var connectionString = $"{connectionData.AnvaTestDatabase}uid={connectionData.AnvaTestDatabaseUid};pwd={connectionData.AnvaTestDatabasePwd}";

      optionsBuilder.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
    }
  }
}

When I want to create the migration file like this in package manager console:

add-migration initialMigration

I get this:

Unable to create a 'DbContext' of type ''. The exception 'Object reference not set to an instance of an object.' was thrown while attempting to create an instance.

What am I doing wrong? Do I need to add something in program.cs to initate the dbcontext?

I have been searching on google all evening, but no clear examples.

0

There are 0 answers