Skip to content

Multiple db context and keeping single resource #1532

Closed
@AnushreeSoman21

Description

@AnushreeSoman21

SUMMARY

I would want to use a multiple db context approach along with JSON API dot net core and in my case, I want to use 2 db contexts 1 for Postgresql and 1 for sql. I want to use the same entities but with some differences in db types as used for Postgresql / SQL Db

STEPS TO REPRODUCE

With the below code, If I want to use a single resource 'ResourceA' in both db contexts, the program gives an error and does not allow me to add same resource in the Program.cs file

// Program.cs
builder.Services.AddDbContext(options => options.UseNpgSql("Data Source=A.db"));
builder.Services.AddDbContext(options => options.UseSqlServer("Data Source=B.db"));

builder.Services.AddJsonApi(dbContextTypes: new[] { typeof(DbContextA), typeof(DbContextB) });

builder.Services.AddScoped<IResourceRepository, DbContextARepository>();
builder.Services.AddScoped<IResourceRepository, DbContextBRepository>();

Can I able to use the same Resource/Entity with multiple db contexts and just ignore the properties that do not support them?

Can you please help me with this with some examples?
Note: I do not want to change the API endpoint for the ResourceA with 2 db contexts.

VERSIONS USED

  • JsonApiDotNetCore version: 5.5.1
  • ASP.NET Core version: 8
  • Entity Framework Core version: 8
  • Database provider: PgSql and SQL

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions