Creating a database context using the database first approach with entityframework core.

asp.net-core dbcontext entity-framework-core

Question

I want to be able to create a database context with entityframework core in my webapi project using the database first approach. When I create like this it works very well

    public class TestingContext : DbContext
        {
            public TestingContext(DbContextOptions<TestingContext> options)
                : base(options)
            {
            }

            public TestingContext()
            {
            }

            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                optionsBuilder.UseSqlServer("Data Source=xxxxxx;Initial Catalog=xxxxxx;Integrated Security=False;User Id=xxxxx;Password=xxxxx;MultipleActiveResultSets=True");

            }

            public DbSet<Information> Information { get; set; }
            public DbSet<ArticleUser> ArticleUser { get; set; }

        }

I had to add the line services.AddDbContext to make it work.

     public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddCors();
            //using Dependency Injection
            services.AddSingleton<Ixxx, xxx>();

            // Add framework services.

            services.AddApplicationInsightsTelemetry(Configuration);
            services.AddDbContext<TestingContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            // Register the Swagger generator, defining one or more Swagger documents
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info { Title = "Articles API", Version = "v1" });
            });

        }

If I remove this method from my TestingContext

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("Data Source=xxxxxx;Initial Catalog=xxxxxx;Integrated Security=False;User Id=xxxxx;Password=xxxxx;MultipleActiveResultSets=True");

    }

I get the error below.

No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext.

Why do I need to pass my connection string to the database in two places before it can pull my data. Please assist. I am new to the core. The two places are configure services method and the context itself.

1
0
8/12/2017 10:01:34 PM

Popular Answer

If you are creating tests, do you need a backing Sql database? Would the In-memory provider not serve you better?

options.UseInMemoryDatabase("database-name");

For this reason, I'd ditch using the OnConfiguring method, and rely on passing the DbContextOptions to your constructor

Side note, you have to consider what you are testing - are you testing your code that is dependent on your DbContext, or are you testing your DbContext itself - if there is no custom logic and you are merely extending the DbContext, there may not be enough value in writing tests for it - and you're not responsible for testing EFCore itself.

0
4/15/2019 10:20:02 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