How to write a EF Core 2.0 DbContext which can be used in WPF and Web.Api.Core

c# entity-framework entity-framework-core

Question

i am using this two Context for WPF and Web-Api. The point is with EF6, i could write an overload of the constructor and pass the connectionstring into it for the usage in a WPF-Application. But in EF Core i have to override the Onconfiguring method, this way it can't be used in a Web.Api.Core anymore. Because i want to use dependency injection feature which is registered in startup.cs.

services.AddDbContext<SamuraiContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SamuraiConnection")));

How can i create a context which can be used in both cases?

Thx in andvance.

Context for WPF:

public class SamuraiContext:DbContext
{
    public static readonly LoggerFactory MyConsoleLoggerFactory
        = new LoggerFactory(new[] {
          new ConsoleLoggerProvider((category, level)
            => category == DbLoggerCategory.Database.Command.Name
           && level == LogLevel.Information, true) });

    public DbSet<Samurai> Samurais { get; set; }
    public DbSet<Quote> Quotes { get; set; }
    public DbSet<Battle> Battles { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        var connectionString = ConfigurationManager.ConnectionStrings["WPFDatabase"].ToString();
        optionsBuilder
            .UseLoggerFactory(MyConsoleLoggerFactory)
            .EnableSensitiveDataLogging(true)
            .UseSqlServer(connectionString);
   }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<SamuraiBattle>()
            .HasKey(s => new { s.SamuraiId, s.BattleId });

        base.OnModelCreating(modelBuilder);
    }
}

This is my Context for Web.Api:

public SamuraiContext(DbContextOptions<SamuraiContext> options)
            : base(options)
        { }

        public DbSet<SamuraiApp.Domain.Samurai> Samurais { get; set; }
        public DbSet<SamuraiApp.Domain.Quote> Quotes { get; set; }
        public DbSet<SamuraiApp.Domain.Battle> Battles { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<SamuraiBattle>()
                .HasKey(s => new { s.BattleId, s.SamuraiId });
            base.OnModelCreating(modelBuilder);
        }
1
0
2/13/2018 9:25:25 AM

Accepted Answer

You can still have a second constructor for your connection string:

private readonly string connectionString = null;

public SamuraiContext(DbContextOptions<SamuraiContext> options)
        : base(options)
{
}

public SamuraiContext(string connectionString) 
{
  this.connectionString = connectionString; 
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    if(this.connectionString != null)
        optionsBuilder
            .UseLoggerFactory(MyConsoleLoggerFactory)
            .EnableSensitiveDataLogging(true)
            .UseSqlServer(this.connectionString);
}
0
2/13/2018 9:41:52 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow