Use EF Core in Blazor WebAssembly Hosted, server side [Answered]

Hi, I've a Blazor WebAssembly Hosted (.NET 6) web app, and I'd like to start using EF Core inside the server part, forwarding then the models to the client part, possibly after having mixed the models in some way. Is this use case supported?
6 Replies
Teddy
Teddy2y ago
For the Hosted WASM project, the client (Blazor WASM) and the host (ASP.NET Core) don't have any communication with eachother, the WASM portion just gets delivered to the browser as static files. SO! Basically you need to turn your Hosted side, the backend project into an API by adding controllers and endpoints to it, along with setting up EF Core, entities, pulling what's needed in the controller, optionally mapping it to a DTO (highly recommended), then request that endpoint from within the Blazor project, deserialize it, then make use of it. You could create a shared class library project referenced by both backend and Blazor WASM projects to contain the DTOs so they can be used by both.
alkasel#159
alkasel#1592y ago
Ok. Yeah I supposed it was possible, it's just that it seems to me that all examples shows how to access EF Core from the the WASM part, which seems a foolish thing to do to me, so I thought that maybe for some strange reason you could not just call EF Core from back-end
Teddy
Teddy2y ago
Yeah, client should never be connecting directly to a database, that's a huge security hole.
alkasel#159
alkasel#1592y ago
ok perfect, glad to hear that Thanks!
Patrick
Patrick2y ago
!close
Accord
Accord2y ago
✅ This post has been marked as answered!