How can I use Union All on different entities using Ardalis CleanArchiteture ?
Example is if I have Product and ProductKit, they have different structures and are different entity.
Is there a way to use Ardalis Repository Specification and union all Product (Id, Name) and ProductKit (Id, Name) ?
The main goal is to search by Name and return only Id and Name from Entities (maybe just a flag isKit to identifiable in results) using Paged results.
I am using
<PackageReference Include="Ardalis.Specification.EntityFrameworkCore" Version="6.0.1" />
Related
I make a dashboard for log viewer system.
I use .net core 1.0 and Entity Framework Core. The log datum are in each tables. And the each tables were named by datetime like [2018_01_01] , [2018_01_02].
I have no idea how to design DBContext class of that log table(dbset) datum.
Because the datum counts and names are dynamically changed. (1 table per 1 day)
But all the log tables have same column format.
How should i design dbcontext class and get those multiple table by entity framework core? If this is not possible to use entity for this case maybe i must use sql...
I'm not aware of a way to dynamically add entities to LINQ, let alone refer to this automatically in code. So here are some solutions:
Application level solution:
Work out the data range you will be selecting
Dynamically build a string like this
:
SELECT *, CAST('2018-01-01' AS DATE) FROM [2018-01-01] UNION ALL
SELECT *, CAST('2018-01-02' AS DATE) FROM [2018-01-02] UNION ALL
SELECT *, CAST('2018-01-03' AS DATE) FROM [2018-01-03] UNION ALL
SELECT *, CAST('2018-01-04' AS DATE) FROM [2018-01-04]
Run the string against a class that matches the columns returned
:
MyClass t = db.Database.SqlQuery<MyClass>("sqlquery");
You are probably going to run into various issues here when you get too many tables.
Database Level Solutions
Use DDL and DML triggers to automatically write the data to a single table and just use that single table
Create a view that has to be altered every day that contains the union select statements above
Ideal Solution
Redesign the database
I realise this is not an option but you should know that you have to implement extra effort and coding because this database is not designed properly
The SQLite authors currently implemented a Union Virtual Table, but I dont understand the benefit of using that over a simple: CREATE VIEW foo AS SELECT * FROM A.foo UNION ALL SELECT * FROM B.foo? Wouldn't the latter preserve index queries on those tables and also allow adding write functionality via triggers?
I need to assign one of multiple parent types to a single child item. The problem I encounter is that in an Access 2010 web database I cannot create a Union query to bring all the potential parents (from multiple tables) into a single drop down / listbox.
I'm a bit green to all this and could be going about it completely wrong. I'm very open to suggestions. Here is my example:
Contracts are the parent of Subcontracts.
Both Contracts and Subcontracts have a Statement of Work (SoW).
Contracts and Subcontracts can both be direct parents of a SoW.
Each SoW will have only one parent
SoWs are split into paragraphs (not overly consequential)
With a union query I would build the database this way:
Contracts table
Subcontracts table
Union table for contracts and subcontracts
Lookup to union table from SoW table in order to select either a contract or a subcontract as parent from a single data source.
The problem here is that I cannot create a union query in a web database.
My only other thought is to construct the database in this fashion:
Contracts table
Subcontracts table
Contracts SoW table
Subcontracts SoW table
This design (using two tables) might work more effectively for data entry as there could be issues with subforms when attempting to use a union table. I'm not sure as I haven't yet tried. With this method, the Access report should be able to bind the subcontract to the parent contract and display all data in a detail section. However, this design still means that I will use two separate tables to house identical data.
I would put the two contract tables together into one table that would look something like this:
CREATE TABLE ContractTable(
ContactID INTEGER NOT NULL PRIMARY KEY, -- Possibly an autonumber
[various contract columns],
ParentContract INTEGER
);
Note, I know this is not Access friendly syntax. I usually use bigger DBs, but you should be able to get the idea.
Then your query to find parent contracts is SELECT ... FROM ContractTable WHERE ParentContract IS NULL.
To find sub contracts SELECT ... FROM ContractTable WHERE ParentContract IS NOT NULL.
My concern with this approach is that if you need to search through chains of contracts (i.e. A parent of B parent of C parent of D, and you need to go from A to D), you could run into recursive SQL which I don't think Access can handle. You'd have to do it VBA code.
You have a query say :-
select p from profiles p, group g where p.profileId = g.profileId
How will you implement it using JDO. It could be basic but I am new to JDO and was not able to goolge something meaningful.
In JDOQL you dont really have something like a JOIN. As #sihaya mentioned, retrieving a object will also fetch all of its members (depending on the configured fetch type ("eager" will load everything and "lazy" will load it later))
Looking at the docs, you can use something similiar, called "unbound variables":
Unbound variables, which serve as a replacement for the JOIN operation of SQL
Unbound variables are supported by ObjectDB, but considered optional by JDO. Queries with unbound variables are similar to JOIN queries in SQL because every combination of variables has to be checked for every candidate object. Just like JOIN queries in SQL, queries with unbound variables may become very slow, so caution is needed when using them.
Here is the link: https://www.objectdb.com/database/jdo/manual/chapter7
In JDO and other object-relational mapping frameworks such as JPA the join condition for a relation between two entities is inferred from the meta-model.
In your example you presumably have two entities Profile and Group and a n-1 relationship from Group to Profile. Then given the following entities:
#PersistenceCapable
public class Group {
private Profile profile;
...
}
and
#PersistenceCapable
public class Profile {
...
}
Executing the following JDO query will select all profiles that are referenced by a group:
select g.profile from Group g
I must be missing something, when trying to define a HABTM association with 2 of my models. I have a "Product" table, and a "Category" table, and a "ProductsCategories" join table. In SQL Server, I am defining the Relationship between the tables on the join table. However, when I create the LINQ to SQL model "Product," I get "Product.ProductCategories -> ProductCategory, ProductsCategory.Product -> Product" Is it possible in asp.net mvc to define a relationship that would give me Product.Categories or Category.Products?
Someone is surely wiser than me on the subject, but I don't know a way of doing this [automatically] if you are using a DBML file to autogenerate your database model. By knowing you can use Product.ProductCategories.Category and Category.ProductCategories.Product, you can create partial classes for Product and Category that define properties named Products and Categories which return Product.ProductCategories.Category, and Category.ProductCategories.Product.