I'm using EF 4.0 and was able to create self referencing many to many relationship. Person and family members. What I also want is to add additional attribute like 'mother', 'brother', 'sister' for each relationship. At the database level, this model generates two tables. Person and PersonRelationship. PersonRelationship table has person_id, and relative_id as PK. I like to have another column relationshiptype in PersonRelationship table and reference in EF 4.0. Please let me know how to do it if you don't mind?
If I were you I would create a table "RelationshipTypes". It holds the different possible types of relationship for you. Just two columns ID and Name of relationship.
Second you just add the RelationshipTypeID to your PersonRelationship and mark it as primary key too.
You should have then three primary keys in your table. Just update your model in Visual Studio and it should work.
Does this help?
Regards Thomas
Related
I'm using CoreData for the first time in one of my project. My table involves FOREIGN KEYS and since CoreData doesn't support FOREIGN KEYS, I'm having some issues.
Below is the structure of my tables.
My Problem is the establishment attribute.The establishment attribute is supposed to hold the name of a particular facility from the Facilities table. However, since it's a relationship, Xcode expects a Facility rather than just a name of a facility (NSString).
Is this possible, or am I just mixing up FOREIGN KEYS with RELATIONSHIPS in CoreData? How would I solve this problem?
Thanks in advance.
A relationship is not a property, so it does not have a type. In the model editor you add a relationship explicitly.
From your diagram, I see that you did not set the inverse relationship. There needs to be a corresponding relationship from name to the Assessors entity. (Set the "Destination" to Assessors in the model editor.)
I would also suggest to rename a few items.
First, use singular: Assessor, Facility. These are objects (comparable to classes), not tables.
Second, because your name attribute refers to an assessor, call it assessor, and similarly call its reverse relationship facilities (it is a to-many relationship in this direction, so the plural is appropriate).
If you need the name of the assessor of a facility, you use
facility.assessor.name
This should make it obvious why you do not need foreign keys. Indeed, I would urge you to think that it is not Core Data that does not support foreign keys, but that it is the traditional relational databases that do not support relationships!
I have 2 entities in my domain that have a one-to-one relationship with one another.
Entity Business has a Promotion. And Promotion has a Business. I know this doesn't make a lot of sense and I could as well integrate the fields of Promotion into my Business entity, but I'm dealing with a legacy database here and I'm trying to model my domain on top of it without changing anything in the database for now.
My problem is that although the relationship goes both ways, it's only stored on the side of the promotion table that has a business_id foreign key, but there is no promotion_id foreign key in the business table. So when I try to run my app, Grails can't find the promotion_id column it expects to find for the promotion field in Business.
Is there any way to model my domain entity so that it understands that the column for the relationship is in the destination table only?
Right after I asked this question, I found my own answer: using hasOne as explained in http://grails.org/doc/latest/ref/Domain%20Classes/hasOne.html
I have a problem in my project.
In one database relations have a join to self with many to many.
What any of people solve this problem in Entity FrameWork?
Any many-to-many relationship should create a new table to represent the pairings.
Example: say you have a table People, and you want to show who gave birthday gifts. A person can give gifts to many friends, and a person may receive gifts from many other people.
CREATE TABLE People (person_id INT PRIMARY KEY);
CREATE TABLE GiftGiving (
from_person_id INT,
to_person_id INT,
PRIMARY KEY (from_person_id, to_person_id),
FOREIGN KEY (from_person_id) REFERENCES People(person_id),
FOREIGN KEY (to_person_id) REFERENCES People(person_id)
);
Re your comment:
For EF implementations, see these related questions:
Many to Many self Join with Entity Framework Code First
Entity Framework many-to-many self-reference
Self-referencing many-to-many recursive relationship code first Entity Framework
I simply put an ICollection to itself. Let EF handle the db layer.
public class Person{
public virtual ICollection OtherPersons {get;set;}
}
I have two tables A and B in my db with a foreign key between both. Table A has a PrimaryKey and UniqueKey. Table B is referencing Table A by using the UniqueKey. If I generate the ef model from the database then no association between A and B is generated and I couldn't find a way to manually add the association. However, Linq2Sql recognizes the relationship as expected.
Any ideas?
See this MSDN blog post: http://blogs.msdn.com/b/efdesign/archive/2011/03/09/unique-constraints-in-the-entity-framework.aspx. In brief, this isn't supported in V4, though the EF team seems to have plans to support it in future releases.
I am one of hibernate user in Australia. Recently, I have been dealing with hibernate many-to-many mapping and not going well though.
I got in trouble with "join/associate table with extra columns mapping".
Let`s say there are three tables called Product Order and OrderProduct (includes extra column quantity). Product holds many-to-many relationship with Order.
My confusion is that do we have to consider both ends of associate table when we are writing mapping files? or just write either side?
Also, is it necessary to produce mapping file for the associate table as well?
Any suggestions will be appreciated!!
create a view to join two tables. this view and rest table is many to one relationship
Hope it's userful
You should have an association mapping and entity because you need to create them just like an other entity.