How to access Entity Framework DbContext entities in Server-Side Blazor Components - entity-framework-core

I'm new to .NET Core and Blazor, with mostly WebForms and MVC experience.
All the Blazor documentation and tutorials I've found use a separate API project and access data through HttpClient and Json serialization/deserialization. I see why this would be necessary for client-side Blazor using WebAssembly, but for Server-Side Blazor using SignalR what's the best way to access the database directly from the components' .razor files using an Entity Framework DbContext?
For example, in an MVC controller you can just do something like:
private ApplicationDbContext context = new ApplicationDbContext();
and then query the data by doing something like:
var things = context.Things.Where(t => t.ThingAttributes == something);
Is there an approach that is this clean and efficient when working with components in server-side Blazor?
Sorry for the broad nature of this question, feel free to point me to blogs, docs, or tutorials I should have already read. Thanks!

What you call a controller should be turned into a service class, that retrieves data from the database, and pass it to the calling methods. You should add this service to the DI container in the Startup class. To use this service in your components you should inject it like this:
#inject DataService myDataService
I think that the Blazor templates come with sample how to define such a service and use it in your components.
Here's a link to a sample by the Blazor team how to create a service and how to use it in your components. The service doesn't use Entity Framework, but this is something really minor I'm sure you'll cope with.

Related

Blazor w/ Entity Framework Core - compile error

I have the following setup but am unable to finish building as I get an obscure error related to line 439 in file Blazor.MonoRuntime.targets (MSB3073).
Does this essentially mean that Entity Framework Core will in no way work with Blazor preview 6?
Details:
Asp.net Hosted Blazor
AspNetCore.Blazor (3.0.0-preview6.19307.2)
Microsoft.EntityFrameworkCore (3.0.0-preview6.19304.10)
Microsoft.EntityFrameworkCore.Design (3.0.0-preview6.19304.10)
Microsoft.EntityFrameworkCore.SqlServer (3.0.0-preview6.19304.10)
Resolved via a hack solution!
Somehow I was able to resolve everything and makes things run
end-to-end. I believe the big, critical thing was:
* Ensure that the Blazor client AND server projects do not directly reference Entity Framework
* Do not let the Blazor client reference (directly or indirectly) the project with the generated entities). To get access to the models, I
just create a duplicate of the generated entities (and removed the
"partial" from the classes that were generated)
Some clarification is needed here, right:
You cannot use Entity Framework on the Client project of Blazor. Entity Framework is a server technology.
You may use Entity Framework on the Server project of your application.
Communication between your Client side and Server hosting side is ordinarily done via Http calls (HttpClient service), but you may also employ SignleR.
To enable Http calls you should expose Http routing endpoints... This can be enabled by using Web Api with the required endpoints. Your Web Api exposed methods (Controllers' methods) can access the database directly (or indirectly if you define repositories, services, etc) via Entity Framework objects, and return the queried data to the calling methods (HttpClient methods).
Note that in my answer I particularly relate to Blazor Client-side apps, but it is mostly true with regards to Blazor server-side apps. I may just add here that in Blazor server-side apps you don't have to use Web Api since Blazor is executed on the server. In such a case, you can define a normal service to retrieve the data from the database, and pass it to the calling methods (no HttpClient involved here).
The Shared project intended to contain objects that can be used by both the front end and back end. This is the place where you can define your Model objects. As for instance, you can define an Employee class that can be used to retrieve the data and pass it to the Client as a list of Employee objects, and in the Client you can define a list of Employee objects that will store the retrieved data. In short, you don't have to define two types of objects, one appropriate to the server, and one appropriate to the client (say your client is an Angular app).
Hope this helps..

Is it better to use POCO objects or detached EntityFramework object to expose database via WCF?

I created a WCF service in charge of exposing my database's data since I don't want the database to be directly accessed by my application (for security reasons) and i need to be able to share data with third-party applications.
My solution is structured this way: WPF application -> WCFService library -> DataAccessLayer library. (Arrows define assembly dependencies 'depends on')
To implement the WCF service I considered to simply return detached EntityFramework objects from the service BUT it forces the main application to have a dependency on the DataAccessLayer library.
The only way i can get around that is generating POCO objects and use them to send them over the wire, but now i have to map values back and forth EntityFramework.
At the moment i'm generating POCOs dynamically via a T4 template and I'm using AutoMapper to map values back and forth EntityFramework.
The Wcf service will just have to implement the repository pattern to expose data.
Is this a good solution? Are there any other option?
Is there any shortcoming i should be aware of?
Depending on your constraints, I would have to agree with this solution.
I created an almost identical solution, although our motivations were slightly different. Our client was Delphi Win32, and at the time didn't have good support for JSON, so we had to use SOAP.
The client also didn't support nullable primitives, so the POCOs removed all unsupported types, and performed other changes to ensure interoperability, then we used Automapper custom mappings to handle the two way conversions.
All the WCF services (contracts and implementations) where also generated by T4 templates, using a generic repository. With T4 templates, I was able to generate a separate WCF service per table for CRUD operations, and then manually created WCF services that were business specific.
Finally, I was also able to used T4 templates to generate the Delphi repositories that interacted with the SOAP services.
Or
You could just as easily move the POCOs (and code generation) to a separate project, change your DataAccessLayer library to reference the POCOs library and only contain the Db context made up of DbSets of your POCOs, and Data access logic but no entities (which are now POCOs). Your clients will not need to have a dependency on the DataAccessLayer library.
So... a good solution, depending on your constraints.

Using WebApi + Odata on an Edmx

We are currently looking at converting from WCF Data Services to WebApi with Odata. Our entity model is defined using an entity framework defined as an edmx. Im struggling to get the edmx working with WebApi OData due to relationships and complex properties.
I'm just wondering whether someone has successfully implemented webAPI with odata on a bigger sized edmx (that has relationships as well)? Any advice would be great.
You may try using RESTier -- a .Net framework built upon Web API OData. There are several things you may need to pay attention:
RESTier has an EF provider which is quite similar with WCF data services. So it should work wiht the edmx model with little tweak.
RESTier is not a "competitor" for Web API OData, it's built upon Web API OData and can fallback to Web API OData.
RESTier currently is a preview version, but it has good support for the common features used of OData service.
If you tried out and find it cannot work, you can create an issues on https://github.com/odata/restier/issues with more detailed information, if you successfully make it work, it will be great you share your experience.

end-to-end RIA-like client/server patterns? non-Entity Framework contexts?

I have posted this same question in the msdn forums, but nothing yet ..
http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/60cf36d1-c11a-4d8a-9446-f1d299db1222
I'm working on a project that is an MVC app that will be sourced data via a WCF service that may or may not be getting data via EF, but will definitely be using Stored Procedures..
The MVC app will maintain state in the session, and the entity-tracking portion of this state would preferably function much like the RIA Services DomainContext. Whether or not this context encapsulates saves and changesets is not really all that important, but how entities are loaded into the context and relate to one another (navigation properties) are.
Question 1: Is there such a pattern/solution in existence?
Question 2: Should the MVC and WCF layers share the same DTOs/Entities via a class library? (thereby maintaining state-awareness, navigation properties, etc on both ends of the pipe?)
Question 3: Does using WCF Data Services help solve these problems?
Question 4: Is this all misguided and is there a better approach?
Pretty basic stuff here..
The solution is use a WCF Data Service, and in the client add a Service Reference pointing to it. The client-side proxy will include a proxy and the context classes I was looking for, similar to RIA. If you're accustomed to RIA, there will be some differences and caveats, but by and large it's easy to work through and provides a client-side proxy to your server-side ObjectContext (or whatever repository you expose through the DataService)

Where to put Service/Data Access Classes in a Zend Framework App

I originally wanted to find out how to access the Doctrine 2's Entity Manager from within Entity Classes. But I saw another question Using EntityManager inside Doctrine 2.0 entities, and learnt that I should be using a service class. I wonder where should I put in a Zend Framework Application? Also is it also called a DAO (Data Access Object)? I am thinking of naming it DAO instead of Service as Service sounds alot like something for external sites to use (like Web Service)?
I am thinking something like Application_Models_DAO_User?
Service classes are part of the autoloader mapping. Like Application_Model_Something can be found in application/models, it's the same for services.
An application service Application_Service_Something should be located at: application/services/Something.php
When you use service classes inside modules, for example Blog_Service_Something they need to be located at: application/modules/blog/services/Something.php
I think classes like entity managers shouldn't be part of your controllers nor your models, but located in service classes.