I'm not sure what is considered the "best practice", but I've always just used a single DbContext in the application. I've ran into situations in server-side Blazor where I've had to implement a DbContextFactory to avoid circuit errors, so if needed, it may be a better approach to implement a DbContextFactory. Jeremy Likness has a good and detailed blog post related to building a WebAssembly app that touches on DbContextFactory and does a much better job at explaining it that I can. Check it out at: https://blog.jeremylikness.com/blog/build-a-blazor-webassembly-line-of-business-app/
Using multiple DBContext's in the default .NET 5 Blazor Webassembly Project?
Hello Everyone,
Just started with Blazor WebAssembly, really love it, thx!!
This may be a dumb question, is it best practice to use multiple DBContext's in the default .NET 5 Blazor Webassembly Project (3 Projects Server, Client, Share) or should I just have the one default ApplicationDbContext?
Here is what I have so far:
The ApplicationDBContext (Default, all my Identity Data is in my MSSql Database)
public class ApplicationDbContext : ApiAuthorizationDbContext<ApplicationUser>
{
public ApplicationDbContext(
DbContextOptions options,
IOptions<OperationalStoreOptions> operationalStoreOptions) : base(options, operationalStoreOptions)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<ApplicationUser>().ToTable("Users");
builder.Entity<IdentityRole>().ToTable("Roles");
builder.Entity<IdentityUserRole<string>>().ToTable("UserRoles");
builder.Entity<IdentityRoleClaim<string>>().ToTable("RoleClaims");
builder.Entity<IdentityUserClaim<string>>().ToTable("UserClaims");
builder.Entity<IdentityUserLogin<string>>().ToTable("UserLogins");
builder.Entity<IdentityUserToken<string>>().ToTable("UserTokens");
}
}
The Startup Configuration also default:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDatabaseDeveloperPageExceptionFilter();
services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
...
}
...
}
Having now also a Product Model and MSSQL Table of Product, is it best practice to add a new DBContexts for Product and other further Entities as shown below or could I just add the Entities (Product) to the existing ApplicationDbContext?
public class ProductContext : DbContext
{
public virtual DbSet<Product> tblProduct { get; set; }
}
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
...
services.AddTransient<IProduct, ProductDataAccessLayer>();
...
}
public interface IProduct
{
public IEnumerable<Product> GetAllProducts();
}
public class ProductDataAccessLayer: IProduct
{
ProductContext db = new ProductContext();
public IEnumerable<Product> GetAllProducts()
{
return db.tblProduct.ToList();
}
}
[Route("api/[controller]")]
public class ProductController : Controller
{
private readonly IProduct objproduct;
public ProductController(IProduct _objproduct)
{
objproduct = _objproduct;
}
[HttpGet]
[Route("Index")]
public IEnumerable<Product> Index()
{
return objproduct.GetAllProducts();
}
}
1 Posted 3 years ago by selliott Edited 3 years ago0 Thank you so very much, certainly appreciate your feedback! Thanks, Kind blessings, Andreas
Posted 3 years ago by Andreas