How to split Context into multiple files?

Asked

Viewed 22 times

1

I am refactoring a large project that has about three hundred tables using . Net Core and Oracle.Entityframeworkcore With scaffold it generates me a Modelcontext. However, it would be huge if I did all the entities in this single Modelcontext. I thought about injecting a context into the dependency and everyone else would inherit it from it but the controller didn’t recognize it. For example, I created this context:

using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

namespace webapi.Models
{
    public partial class TesteContext : DbContext
    {
        public TesteContext()
        {
        }
        public TesteContext(DbContextOptions<TesteContext> options)
            : base(options)
        {
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
        }
    }
}

And this other to inherit from above:

using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

namespace webapi.Models
{
    public partial class ModelContextImoImovel : TesteContext
    {


    }
}

And I tried to inject the dependency like this into Starutp.Cs:

        public void ConfigureServices(IServiceCollection services)
        {
            var connectionString = Configuration.GetConnectionString("DefaultConnection");

            services
            .AddEntityFrameworkOracle()
            .AddDbContext<TesteContext>(options => options.UseOracle(connectionString));


>services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        }

But at execution he returns to me

Unable to resolve service for type 'webapi.Models.Modelcontextimoble' Any suggestions? Thank you.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.