Vitor Canova
Vitor Canova

Reputation: 3976

Is it possible to auto generate database using two Context?

I'm using Entity Framework Code First and ASP.NET Identity. In this case the ASP.NET related tables are auto generated when its ApplicationDbContext is used the first time.

The problem is that I wanted to create a separate DLL and use the same database and the new classes should create tables in the same Context.

I created a new DbContext and I'm using the same connection name.

Unfortunately EF does not understand that I want my DbSet tables in the same database as the ApplicationDbContext.

When It reaches a line of code where I'm using the tables I wanted be auto generated a message is shown saying this:

Npgsql.NpgsqlException: 42P01: relation "public.Customers" does not exist

this is a sample code:

var customer1 = (from u in context.Customers
    where u.Id == 1234
    select u).ToList();

Is it possible to use auto generation of the database in EF Code First with more than one DbContext?

Upvotes: 0

Views: 98

Answers (1)

Shahed C - MSFT
Shahed C - MSFT

Reputation: 2881

Yes, it's possible. Here's a slightly old tutorial:

Entity Framework 6 Code First Migrations with Multiple Data Contexts

It specifies 2 scenarios: multiple DbContexts within the same project, and in separate projects.

Hope that helps!

Upvotes: 1

Related Questions