Different database context for same EF model (database)?

entity framework multiple databases (same schema)
entity framework multiple context instances
entity framework dbcontext multiple databases
entity framework core dbcontext multiple databases
ef core multiple dbcontext same database
entity framework multiple database providers
multiple dbcontext entity framework core
what is dbcontext and dbset in entity framework

Is possible to add another database context for the same EF model (database)? I have a situation where the db context must change depends on what kind of user is logged in.

In a nutshell - YES. The underlying MODEL is simply a class representation of your database tables, so the trick here is to make the connection by using a different database driver as presented by Entity Framework.

EG: Implement a repository pattern inside a class and call it MySqlContext, where this class inherits from DbContext, and this will have the OnConfiguring method:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        using (var secretsClient = new InsightSecrets())
            var connectionInfo = // here you add your own depending on environment and project
            string connectionString = connectionInfo.ConnectionString;


The optionsBuilder instance can then create in this case above, either a MySql or Postgress reference - while this repository will then be instantiated higher up in your code by doing something like this inside a class constructor, where this context will be used:

var ctx = new MySqlContext();

The repository called MySqlContext can in this example already provide either MySql or Postgres, but nothing stop you from implementing another repository that provides a different database context depending on the associated db drivers.

DbContext in EF 6, Entity Framework - Multiple DbContext - In this chapter, we will be learning how In our example, we will define two Context classes for the same database. Once it is executed, we will add the model in the migration history and for that, we​  Do you want a different DbContext class or just a different database? The DbContext class is a bridge between the database and your entities (EF model). This bridge can be used to transfer data from/to different databases. Creating a different DbContext class for the same EF model doesn't make much sence to me; unless i'm missing something

Do you want a different DbContext class or just a different database? The DbContext class is a bridge between the database and your entities (EF model). This bridge can be used to transfer data from/to different databases. Creating a different DbContext class for the same EF model doesn't make much sence to me; unless i'm missing something...

But you can do both. If you're looking for a another connection to a database, then this would be your DbContext class, your "bridge":

public class TestContext : DbContext
    public DbSet<Foo> Foos { get; set; }
    public DbSet<AnotherFoo> AFoos { get; set; }

    public TestContext() : this("TestDB1") { }

    public TestContext(string databaseName)
        : base(databaseName)
        Configuration.LazyLoadingEnabled = true;

The DbContext builds the connection with a database providing the database name in the base constructor. Therefore you can control the database it connects to through the constructor.

The databases' connection strings must be declared in the .config file of course. For example:

  <add name="TestDBContext1" connectionString="Data Source=.\SQLEXPRESS; Initial Catalog=TestDB1; persist security info=True; user id=sa; password=saexpress;" providerName="System.Data.SqlClient" />
  <add name="TestDBContext2" connectionString="Data Source=.\SQLEXPRESS; Initial Catalog=TestDB2; persist security info=True; user id=sa; password=saexpress;" providerName="System.Data.SqlClient" />

My advice would be, though, not to use directly the constructors, but rather use the factory design pattern to create the appropriate instances of the DbContext.

Database First, Entity Framework 6: One entity data model, multiple databases We have an application that uses multiple databases sharing the same schema. The generated DbContext doesn't allow to specify the connection string to  Each one is associated with a particular corresponding context class, i.e., Student is associated with MyStudentContext and Teacher is associated with MyTeacherContext. Here is the basic rule to migrate changes in database, when there are multiple Context classes within the same project. enable-migrations -ContextTypeName <DbContext-Name-with-Namespaces> MigrationsDirectory:<Migrations-Directory-Name>.

In Entity Framework, the DbContext class represents a certain database layout. The DbSet<TEntity> in the DbContext represent the tables in this database; the non-virtual properties of TEntity represent the columns of the table and the virtual properties of TEntity represent the relations between the tables (one-to-many, many-to-many, ...)

If you want a different instance of the database, with exactly the same layout, it is enough to create a different instance of the database with a different 'NameOrConnectionString` as input parameter in the constructor.

If you want a very similar database, where you want to re-use some tables and relations from the other database, you should create a different DbContext class; after all: the DbContext class represents your database layout, and a different layout means a different DbContext.

It could be that in this second DbContext you also want different relations, of different string lengths, different table names, (etc) than the default entity framework values. In that case it is better to use Fluent API in your DbContext.OnModelCreating, instead of attributes in your entity classes. This way you can re-use the same entity classes while giving them different column names, string lengths, decimal precisions etc.

What is the purpose of a db context class in asp.net mvc, How do you update a database model in Entity Framework? Working with DbContext. 10/23/2016; 3 minutes to read; In this article. In order to use Entity Framework to query, insert, update, and delete data using .NET objects, you first need to Create a Model which maps the entities and relationships that are defined in your model to tables in a database.

. Expand the Tables, Views, and Stored Procedures nodes, and check the objects you want to add to the . edmx file. Or they may initially use the Database-First approach against the model targeting one DBMS, then switch to the Model-First approach, change the target database by changing the connection and the

Learn how to create an Entity Data Model in Entity Framework 6. Let's create a simple EDM for the School database using Visual Studio if the table has a one-​to-many or a many-to-many relationship with other tables. Visual Studio cannot display the model in Design view and in XML format at the same time, so you  Select required database objects you want to manage (in this example there is only a table named “People”) Rename the model namespace “SampleDB1_Model” to “SampleDB_Model” then click on Finish button. Our model is created, a class named “SampleDB_Entities” inherits of DbContext.

Manage Relationship: Manages relationships using CSDL, MSL and SSDL in Db​-First or Model-First approach, and using fluent API configurations in Code-First  This post is about how to use EF Core in a separate class library. And how to use it in the ASP.NET Web API and how to run migrations to create database. One of the common design approach is split the application in to different layers or tiers. In this post, I will help you to split your models from MVC or API application and consume it.

  • Yes. But if this is Code-First you would enable Migrations or database Initialization on only one of them, of course. You can even have a master DbContext that has all your entities that you only use for Migrations..