EF Core 7 query performance when using TPC with multiple child tables - entity-framework-core

I have a set of business objects that consist of a main object with some properties and a number of child components that are added to the List<BaseComponent> in the MainObject (MO).
BaseComponent is abstract and defines some common properties. Components inherit and also define own properties that are very different from one component to next. About 30 different components exist and which are added to which MO is arbitrary.
For persistence I use EF Core 7 (SqlServer) and TPC (Table-Per-Concrete-Type) approach so that each component child is stored in its own table and has an FK to the main table row.
Simple enough, so far so good. Persistence layer works, Ef Core picks up and materializes MOs with correct components in the List and so on.
My concern here is the innerworkings of the query mechanism, I'm not a database expert, I do have some knowledge of relational databases and SQL in general, but I can't for the life of me figure out how EfCore queries the child tables to only pickup components that belongs to a specific MO (efficiently that is).
Suppose I have a MO, I add five components and save it. MO is saved to its table and components to each their own tables using FK to refer to that MO row.
But the MO row does not contain any information about which components it has, components on the other hand have a FK column with MO row ID.
Does EF Core have to query all 30 component tables to find component rows that refer to a specific MO row ? Or how does it find correct ones ?
My projected growth is circa 1 million MO row/year with any number of child components, I don't know how this will impact the performance of the data layer

Related

Is it possible to associate entities in different models?

We have one DB with many tables. We decided to create different models instead of just one containing all the tables.
We'd now need to associate an Entity (table) in ModelA with an Entity (table) in ModelB. Obviously at DB level this is possible (a simple foreign key) but it looks like it is not at model level.
Suggestions?
Your database is composed of one big model with many tables and relationships
But in your application if you are going to split it into two models then there is a lot of chance of having "Entities" that will be needed in different models.
You can create two different entities instead each representing what is needed from the table in each particular model and map each entity to the same table in Entity Framework
See the image below
The black boxes are your tables, the red and blue are your entites.
As you can see the whole database is related but your ModelA includes only some fields on the common table, while Model B includes its own set of fields from the common table.

Core Data Union Query Equivalent

I want to get the union of data from different entities. I have a number of entities (different kinds of tags e.g. location, events etc) and I want data for a table view that shows "All Tags" (i.e. the union of all tag entities). How do I make a fetch request with Core Data for this kind of a use case?
I know that Core Data is not an ORM but if my explanation above was not good enough, I will explain the corresponding database use case. I have different tables e.g. events, locations, people etc and I would like to UNION the results from these different tables. Remember that a UNION concatenates the rows and not columns.
One obvious solution is to get the data from the different entities separately and then just concatenate together the NSMutableArrays. I am wondering if there is a more efficient way.
There is no way to fetch more then one entity type in a fetch request ...
UNLESS, the entities you like to 'Union' have the same base class in your model (other than NSManagedObject).
So if all your entities were to inherit from a base class named Tag for example, you would be able to fetch all of them together.
This however, will create a unified table for all these entities (a very sparse table if the intersection between the classes is small) in the actual database file.
In your case this might not be feasible as there is no real connection between 'Person' and 'Location' for instance, or you might decide that this will cause a performance issue.
The other solution will be (as you suggested) to create a fetch request for each entity.

Decoupling the Data Layer using Entity Framework

I am using EF 4.0, linq to entities, VS 2010 and SQL Server 2005 Stored Procedures to do a small search application. I have designed the EDM and the required layers. The presentation layer displays the search results properly.
The dilemma now is that the search should be flexible enough to read from different tables. For example, for the present search the application is reading from table A. Tomorrow the application may need to read from table B which may have totally different column names than table A.
Using EDM how can i map table A columns to Table B columns , without having any effect on the presentation layer.
Any suggestions/pointers/links would be greatly appreciated.
Thank you so much for your time and help.
I have used the Data Repository explained in the below link it shows the IDataRepositoryand the DataRepository class. also how you can fetch data dynamically using the fetch() and Find() functions
http://huyrua.wordpress.com/2010/07/13/entity-framework-4-poco-repository-and-specification-pattern/
and its working pretty fine
I would recommend decoupling your presentation layer from your data. Create a business layer with a generic class that can be populated from different tables (entities) depending on your needs.
So, depending on the day, the generic class (or classes) would be populated from Table A, or Table B, or table X. However your presentation layer would be oblivious to this and only aware the data from the generic class.
You could design this a number of ways. One way would be to design an interface that the entities must conform to in order to populate the generic class. So no matter what the table structure is, you would need to map the data in it to the interface in order to populate the generic class and hence display it as search results.

EF CodeFirst: Mapping entities (one-many) to the same table

Lets say I have Customer, Order, OrderDetail classes in the business layer (It's a simplified version of my problem).
I have also an old (existing) database that has one global table where every row of the table contains the information of Customers-Orders-OrderDetails; for example:
CustomerID, CompanyName,Fax,OrderID, OrderDate,ProductID,UnitPrice,Quantity
(in this way there are duplicated information of a Customer in different rows).
In the future I'll have a new database (with different table Customers, Orders, OrderDetails), and I want to use the same program.
I want to use EF CodeFirst to mapping to the old database and in the future to the new database
Which is the best solution?
Design a business layer with a global class that contains the information of Customers-Orders-OrderDetails. So the mapping of this class
with the old database using EF4 in the data layer is trivial.
In the future I'll modify both business layer and data layer for the new database.
Design a business layer with Customers, Orders, OrderDetails classes. In this case is it possible to map these classes to the global table of the old database? How ? (the problem is that the Customer-Order is one to many).
In the future I'll modify only data layer for mapping the new database.
This will work for now and later you will have to modify everything working with a global class - it can be a lot of work.
It is not possible to map one table to three entities where two have one-to-many relation between them with EF.
Use third approach. Load one class as described in first approach but immediately convert result to three classes from the second approach. The reverse operation will be done in case of persisting changes. Wrap this code in single place - repository. Your application will use three classes and it will not have any knowledge about the way how they are persisted. Once you change the database you will only remove additional conversions from the repository and work directly with Customer, Order, OrderDetail loaded and persisted by EF.

One Entity for multiple similar tables

have two tables in database.
They have completely the same columns, only the difference between them - they have different names.
Lets say i have TableSea with column s Id and Name and TableOcean with the same columns Id and Name.
I want to use EF 4 to be able CRUD operations, i am also want to use stored procs mapping for insert update and delete operations.
I am already created POCO entity for first table and i did create stored procedures and map them to entity model. All working well.
How make it work with two tables without create a new entity for second table?
AFAIK, you can't, and you definitely shouldn't!
If you have two identical database tables, then this means one of the following:
The two tables mirror closely
related concepts (like Sea and Ocean
in your example).
The two tables
mirror different concepts which only
accidentally have the same
properties.
Depending on which scenario is closer to reality, you have these two design options:
Merge the two tables and add a
Type property (column), then map
it to one entity type. You might
have different subclasses to
differentiate between types, or you
may go with an additional Type
property - whichever fits better for
you.
Have two tables. Which means: there are two different concepts. Consequently, this has to be mirrored by two different entities in the business model.
In any case, having an entity table in the database means having an entity class in the business model. If there's no such 1:1 - mapping, then clearly something is wrong with the design!
Thomas