For simplicity, I have a database with this tables
Toys
- Id
- Name
Fruits
- Id
- name
Orders
- Id
- Product (contains Fruits.Id or Toys.Id)
- Type (1 if Fruit, 2 if Toy,....)
- Qty
¿how can I map "Product" to store id´s from 2 entities (Fruits or Toys)?
PD: The Id of toys and fruits never match
Thanks
If this is a hard requirement then I would do it by making a view in the database called Products, and it would be a union between Toys and Fruits.
But notice that the navigational property Product, on orders then will be of type Product, and you have to map it to Fruit or Toy manually.
Related
The current scene is this:
There is a category table [category], the number of records is only more than 50, almost no increase, and the modification is rare.
There is a product table [product] currently millions of levels, will always increase.
These two are many-to-many relationships. One category will have more products, and each product will have multiple categories.
The category list is almost not changed, and there are about 1000 products in a category, and the list of a category will be changed not frequently.
Query requirements:
Query all categories (excluding the list of products under the category)
Query the category list by product_id
Query the product list by category_id
Operational requirements:
Modify the product list in category (add/delete a product to a category, sort the product list in a category, so the product list in category needs order.)
How many-to-many design of this kind of scene is better, there are some points:
1. If you follow the design of the SQL database, add a Category<-->Product relation table.
[Question] The order of each category of products is not well maintained. For example, the front-end performs a large-scale adjustment order on a category of products, and then requests it. The Category<-->Product relation table also needs to add an index field to indicate the order, and needs to update a lot of records. It is not particularly friendly to the operation requirements, is there any What can be optimized?
2. The way of NOSQL. Add a products:[] directly to the category to indicate a list of items in this category.
[Evaluation] In the query requirement, there is a requirement to query all categories (excluding the list of products under the category), which will pull out a lot of unnecessary data (products) at one time. Not applicable.
3. Add products:[] in the Category<-->Product association table
[Question] This can meet the operational requirements, but if you want to meet the Query requirments-2 [Query the category list by product_id], how to query it and will there be performance problems?
You need a third table (junction table) to complete the relationship. The keys must be primary keys along with a foreign key constraint.
tblProductCategories
PK product_id FK
PK category_id FK
I am new to the core data and swift, I need to get the unique name and count of particular category which has occur in Product rows.
Here is the core data entity structure.
Product: category:String, name:String, store:String
using core data I want to execute the following sql like query
select category, count(*) as total from Product group by category order by category desc.
Please help me to get it done in swift3 with complete reference.
I have an Item and Person relationship which is a many to many relationship.
I want a person to be able to own 2 different instances of an item (which are represented by the same record in the DB). I want my link table to be able to look something like:
Person ID | Item ID
1 | 3
2 | 4
1 | 3
Unfortunately when a person's items are represented by NSSet. So when I try to set an additional dupe object in the set, I'm guessing NSSet automatically removes it.
Any help?
Create a real entity between the Person and Item entity. Then you can create as many of those "join" entities as you want. Or you can stick another attribute in there such as "quantity" instead of having multiple join entities.
Suppose I have the following three tables:
Products
- Id
- Price
Categories
- Id
- Date
Descriptions
- Type
- Id
- Description
Descriptions is used for Products and Categories. That's why we have the column Type which is the discriminator. For example, for products Type=1 and for categories Type=2.
What would be the appropriate way to map these relationships using code first? I see one option with creating an abstract Description class and child CategoryDescription and ProductDescription classes using TPH. Is there any other option?
Is there a way within the entity framework designer to duplicate an entity and then apply a filter condition to both to make them unique. Id like to retain all navigation properties and what not.
For example, say in the database I had a table of orders. I could have two entities, one called IncompleteOrders and One called Complete based on the same table, with the complete having a filter specified on the database field 'complete'.
Thanks in advance
Yes, this is called Table per Hierachy
You have one physical table, which has a special, single, scalar column which is used as a discriminator.
Like this:
OrderId OrderName IsComplete
1 Foo 1
2 Bar 1
3 FooBar 0
Where IsComplete is the discriminator (BIT column, for example), so when you setup your entities on your EDMX, you create three entities:
1. Orders
2. CompleteOrders (derives from Orders)
3. InCompleteOrders (derives from Orders)
On the table mapping for Orders, you say "Maps to CompleteOrders, when IsComplete = 1", and "Maps to InCompleteOrders, when OrderType = 0".
Good writeup on TPH/Discriminator pattern here.