In our ApplicationDbContext.SaveChangesAsync we have a BeforeSaveChanges method in which we'd like to do the following:
We need a cached list of records which contains the names of certain entities. For these entities we will do an additional check in BeforeSaveChanges. This list will be managed in the UI.
FYI: we're using Mediatr, EF Core, CQRS.
How would you handle this? Any code examples?
Thanks in advance!
Related
Is there a way using Entity Framework Core to get nested data without having to iterate through it? like a simple SQL select with joins i would like to show all data on the same table
Sounds like you know the SQL you need to get the data you want. So I suggest you create a database view and then map your ef entity to that. As long as you are just reading ef will treat the view just like it would any other table.
I think youre looking for .Include() and .ThenInclude() methods that allow you to load related data in a eager way.
More on that topic on the official entity framework documentation at MSDN
I have a web application using J2EE + Spring and a MySQL database. I need one entity which will be read-only. I have one main table with products, and they are only to read. There should be no insertion of new records and no updates currently.
The entity class should only read data and pass the entities forward (other entities are tables like order, shipments etc.).
Is there any solution for this? Does anyone have the same issue? Thanks for the help.
If you don't change an object, it will never be updated.
If you are using EclipseLink you can use the #ReadOnly annotation to mark something as read-only.
I'm researching OData as a RESTful interface for a database. The data is structured in a very unusual way and normal tables and rows do not apply, in fact, some stuff just exists in in-memory collections and objects.
Can I build my own arbitrary mapping system between the entities that make up 'feeds' and the sources behind, this might mean aggregating from sources and building the entities on the fly?
I'm just looking for yes/no (why not) and maybe some pointers to relevant reading material.
Many thanks
Luke
Yes and no.
You can build an OData feed of anything. In a WCF Data Service implementation of same, you can implement IDataServiceMetadataProvider.
However, the easiest way to define an EF data service is:
public class MyOData : DataService<MyObjectContext>
...and that won't work if you need to return non-entity objects. Such services are limited to entities and simple types only.
So yes, you can do it, but it's quite a bit more work than the one-liner above!
Hi
If anyone could please elaborate the difference between the three, i am new to EF and sometimes MergeOption.NoTracking happens to work whereas sometimes ObjectContext.Detach, but i never get the gist of it.
I would like to know Which situations should i use them. Also, if there is an object graph attached to some entity (either by firing the Include function or by calling EntityReference.Load()) What should be called if
1.) i don't want other objects attached to the entity
2.) i want all objects referenced by the entity
Any help would be greatly appreciated.
Regards
Hiren
MergeOption.NoTracking is used to improver performance for loading entities which will not be modified. Entity is in this case is not tracked by the context but it is still attached and lazy loading works.
ObjectContext.Detach completely removes entity from object context scope so the entity is not tracked and lazy loading doesn't work.
IEntityWithChangeTracker.SetChangeTracker is imho more like infrastructure for EntityObject. It is heavily used inside EF when entities are materialized and attached to the context.
I'm looking for something like IEntitesFactory or something similar that allows me to handle the Entities instance creation.
Does anybody knows if it's possible with EF 4.0?
No there is no extension point to include your own entity factory. You can only handle ObjectMaterialized event for entities loaded from.