I have 2 classes that have a common property but are independent of each other.
I would like to create a linkset between the classes but unsure what the best scenario or path is.
class Items
has property "id"
class Item_Images
has property "item_id"
I would like add to Items, a property "Images" which is a linkset to the item_images class.
Am unsure of 2 things.
1) If I am better off creating an edge or if a linkset will suffice.
2) Proper syntax to relate the 2.
Any help is greatly appreciated.
Thanks
It depends, if you want just link the records or link them and add some additional properties on edge. In the last case you should use edges. If you don't need properties on an edge you can use lightweight edges or links for better performance. For proper syntax see theese pages on official documentation:
SQL - CREATE EDGE
SQL - CREATE LINK
Hope it helps.
Related
I'm trying to model the following relationships between entities, mainly consisting of a partial, disjoint generalization.
original EERD
'mapped' to relational
Since I didn't need the subclasses to have any particular attributes I decided to use the "single table inheritance" approach, added the "type" field and moved the relationships towards the parent.
After that I had two choices to make:
1- type for the "business type" attribute
2- way to constraint participation to at most one of the 4 relationships based on the type attribute
For the sake of portability and extensibility I decided to implement no.1 as a lookup table (rather than enum or a hardcoded check).
About no.2 I figured the best way to enforce participation and exclusivity constraints on the four relationships would be a trigger.
The problem is that now I'm not really sure how to write a trigger function; for instance it would have to reference values inserted into business type, so I'd have to make sure those can't be changed or deleted in the future.
I feel like I'm doing something wrong so I wanted to ask for feedback before going further; is this a suitable approach in your opinion?
I found an interesting article describing a possible solution: it feels a bit like an 'hack' but it should be working
(it's intended for SQL Server, but it can be easily applied in postgres too).
EDIT:
It consists in adding a type field to the parent table, and then have every child table reference said field along with the parent's id by using a foreign key constraint (a UNIQUE constraint on this pair of fields has to be added beforehand, since FKs must be unique).
Now in order to force the type field to match the table it belongs to, one adds a check constraint/always generated value ensuring that the type column always has the same value
(eg: CHECK(Business_type_id = 1) in the Husbandry table, where 1 represents 'husbandry' in the type lookup table).
The only issue is that it requires a whole column in every subclass, each containing the same generated value repeated over and over (waste of space?), and it may fall apart as soon as the IDs in the lookup table are modified
I'm recently concerned in the problems we have with Entity Framework and we may need to find a replacement. According to ORMBattle, the best candidate is DataObjects.Net, the result of my initial investigations are very promising, except one feature that we need in our structure:
Consider two classes: Order and Customer, in class "Order" I have a "Customer" navigation property (and probably an "Orders" navigation property in the Customer class). I also need a property CustomerID in class Order.
this is totally possible in lowly EF4.
How can I achieve this goal?
you can add non-persistent property with special getter that does the job:
public long CustomerId
{
get
{
return GetReferenceKey(TypeInfo.Fields["Customer"]).Value.GetValue<long>(0);
}
}
The setter can be added in the same manner.
Hope that helps.
P.S.
This is a copy of the original answer that can be found on the official DataObjects.Net support site.
In a Table-Per-Hierachy scenario is it possible to discriminate on a list of possible values?
e.g. for the types Color, DarkColor, LightColor
something like
Map<DarkColor>(m => m.Requires("TheColor").HasValue(Red || Blue)
Map<LightColor>(m => m.Requires("TheColor").HasValue(Yellow || White)
poor example but hopefully you get the picture!
No, it is not possible.
Entity framework only enables mapping using a intersection of conditions, not a union of conditions. You can see this both in the designer[1] and in the EDMX syntax[2].
* Code first should have the same mapping capabilities.
[2] If you right click the edmx file and choose "Open With..." --> "Automatic Editor Selector (XML)" and try to manually edit the mapping conditions you will see (from the intellisense) that the is no option to enter "OR" between conditions.
First of all, I am not sure if what you want is possible, EF wants to take care of the discriminator column, and based on the class type, it would like to set the discriminator value, in this case, how is it going to set the value, to which possible one. It makes no difference when loading it from the DB, but a little problematic when trying to serialize it to the DB.
1- have you tried doing multiple maps for each possible value:) You might have an error saying DarkColor has already been mapped.
2- 2nd suggestion is adding a [NotMapped] ColorWeight attribute, and returning dark or white based on theColor, and using this property as the discriminator field, but I guess Discriminator field should exist in the Table.
I have a many-to-many relationship between 2 entities in Entity Framework, like here . So, Employees and Projects. At one point, I would like to insert some Projects to a Employees entity in a specific order. By conserving the order I would like to know which was the first preference of the Employees for a Projects entity. The thing is that although I order the Student.Projectslist in the way I like before the insert, when selecting Employees.Projects.FirstOrDefault(), the entities are ordered after the ProjectsId and I don't get the first element I inserted. How can I conserve the order I want?
O course, I could make a new field PreferredProjects and save the other Projects in a random order, since only the preferred one is important for me. But this is not an option, being given the context of the current project's software design.
Thank you in advance...
It sounds like you simply want to have sorted child collection results when you do a query, rather than take full control of the insert order.
You can achieve that using the techniques described in Tip 1 of my tips series.
Hope this helps.
Alex
Program Manager Entity Framework Team.
Unfortunately, there is no easy solution. I have the same problem. The only solution is to save after adding each child item (project). This is the only way to save the order without using a new field column to sort input.
Try Employees.Projects.OrderBy(x => x).FirstOrDefault()
I have the following tables in the database (just a demo, of course):
Positions
PositionId
Limits
LimitId
PositionId
I want to left join them into a single entity (always have position, not always have a limit attached to it):
Position
PositionId
LimitId
I've seen articles regarding one-to-one mapping and "Table per type inheritence", and tried to implement the same method here, but with no sucess. Is that even possible?
I think what you want is an ordinary inner join where your foreign key (PositionID in the Limits table) is allowed to be null.
Yes and no...In my scenario, the 2nd option is the applicable one, since I don't have the same primary key in both tables. so, I must create an updateable view...The problem with updateable view is that I can't modify fields which are in different tables and expect the database to handle it, unless I use "Instead of" triggers, which I really don't want to get into at all...
So, I guess there's nothing out of the box for me...damn.
(Unless you have another idea...)
Anyways, I really thank you for your help, it's much appreciated.
Nir.