How to add mutiple entities for an attribute (iOS, Core Data)? - iphone

I want to create a one to many relationship. My setup is something like:
I have the Profile Entity,
I have the Time entity.
Every profile has a relationship to Time.
How can I define relationships, and add multiple Time entities to a single profile?
I bet it is obvious, but I can't see how to implement.

edit after posting the answer I saw your comment - to define a to-many relationship in the modeller, select the relationship and choose to-many from the options:
To populate the relationship, you can do it two ways. I am assuming your Profile entity has a to-many relationship called times and the inverse relationship is a to-one relationship called profile.
Set the profile on each Time entity as you are creating them. This will automatically populate the inverse relationship (i.e. add the Time to the times set of the profile).
Collect the relevant Time entities in a set and set the times property to this set. Again, the inverse will be automatically populated.
There is more information here. Accessor methods to add individual entities to a to-many relationship can be generated from the managed object model editor in Xcode.

Related

Target/Source and owning/not owning entities

I'm a bit confused about this naming convention.
What is the difference between them and are target/source interchangeable with owning/not owning?
One thing in particular is hard to understand:
"The main difference between a OneToOne and a ManyToOne relationship in JPA is that a ManyToOne always contains a foreign key from the source object's table to the target object's table, where as a OneToOne relationship the foreign key may either be in the source object's table or the target object's table"
JPA wikibooks
I can't imagine such situation in uni one-to-one
Differences between them are a little confusing. You should practice a lot to understand very well.
At first, you should understand some terminology:
Role : In every relationship there are two entities that are related to one another, and each entity is said to play a role in the relationship.
Direction : Relationships can be unidirectional or bidirectional. For e.g.. a Person has an address is normally unidirectional whereas Employee working on a project is normally bidirectional. We will look at how to identify and define directionality while coming up with a Data Model.
In order to have relationships at all, there has to be a way to create, remove, and maintain them. The basic way this is done is by an entity having a relationship attribute that refers to its related entity in a way that identifies it as playing the other role of the relationship. It is often the case that the other entity, in turn, has an attribute that points back to the original entity. When each entity points to the other, the relationship is bidirectional. If only one entity has a pointer to the other, the relationship is said to be unidirectional. A relationship from an Employee to the Project that they work on would be bidirectional. The Employee should know its Project, and the Project should point to the Employee working on it. A UML model of this relationship is shown here. The arrows going in both directions indicate the bidirectionality of the relationship (Form this book >> Pro JPA 2)
Then dive into this link (archived from the original)
I'd like to comment only the links, but I need 50 reputation

Proper way to generate a view from a many to many relationship?

I have two tables - ContactInformation & EmailAddress - which have a many to many (* : *) relationship. After making this association in the ADO.NET data model and generate the db from it, a table titled ContactInformationEmailAddresses which maps the two tables is created in the Entities Container.
Unlike when I scaffold views which have a 1:* relationship, there's no entries available for its counterpart in the view when I scaffold either one and scaffolding off the combined table isn't an option even after updating the model from the db.
My question is simply: is there an automated way to generate the creation form for ContactInformationEmailAddresses that will have the EmailAddress entry field?
At present the scaffolding templates don’t support generating views that require the selection and association with more than one entity – the “many” end of the association.
Make sure to check out this blog article
http://blogs.msdn.com/b/mcsuksoldev/archive/2013/09/20/managing-entity-relationships-with-mvc-scaffolding.aspx

Entity Framework Model first: adding an association without creating foreign key properties?

I'm playing with the Entity Framework model designer, and I've got a question about creating entity associations:
In the "create association" dialog, when I create a 1:many association, it offers this checkbox:
"Add foreign key properties to the [entityname] entity"
I've been checking this box and I get results that are expected and make sense to me: Clicking the navigation property in the diagram highlights the related field in both entities that tie them together.
But, what would it mean not to check this box? I've tried this, and I then see no place in the entity to store a reference to the parent table's primary id. Am I correct that the navigation properties don't store any data in the database? If so, how could this work? Am I, perhaps, expected to manually map the navigation property to an Int32 field on the entity?
Associations represent relationship between entities. In the database (relational model) these relations are modeled by using foreign keys and - in the case of many-to-many - a join table. In the object model relations are typically modeled as references to the related object (in EF they are often referred to as Navigation Properties). The problem arises when you need to create or modify a relationship in the object model - you always need to have a reference of the related object you would like to set. In a pure object model this usually is not a problem but in case of ORM it means that if you don't have the related entity you need to send a query to the database to get the object to be able to set the reference to. However oftentimes - even if you don't have the related entity - you know the Id of the related entity. So, if the foreign key properties were exposed (and handled) in your object model you could create or modify a relationship without having to send additional queries to the database. This is what the checkbox is about. If you check it your entities will have (extraneous from object model perspective) properties mapped to foreign key columns in the database which you can use to manipulate relationships.

Core Data releationship confusion

If I have two entities; Foo and Bar. And Foo has two properties; bar1 and bar2 of type Bar. Now does Foo have a one-to-many releationship to Bar? At least should it be modeled as that in Core Data? Or is it two one-to-one releationship? How do I set that up properly in Core Data with inverse? The one-to-many releationship I understand but not the last releationship type? Is that even possible or good way to that?
And Foo has two properties; bar1 and bar2 of type Bar. Now does Foo
have a one-to-many releationship to Bar?
No, Foo has two relationships with Bar, and those relationships are named bar1 and bar2. Whether they're "to-one" or "to-many" depends on how you specified them. Select each in the model and see whether the "to-many" checkbox is checked.
At least should it be modeled as that in Core Data?
That depends on what you're trying to model. Do you want to have two separate relationships to specific object (that'd be two different "to-one" relationships), or do you want a single relationship to a group of objects (one "to-many" relationships)? Here's an example that might help clarify things...
A business may have zero or more employees. A business also has a single chief executive officer and a single chairman of the board. So the Business entity might have a "to-many" relationship named employees with the Person entity. It might also have "to-one" relationships named ceo and chairman, again with Person. Those might not be strictly necessary -- maybe the CEO and Chairman are both employees, so you could find them by searching the set of employees and filtering by job title. But it can be handy to have them as separate relationships if you're going to use them often and don't want to have to search through thousands of other employees every time you do.
How do I set that up properly in Core Data with inverse?
Again, it'll depend on what you're modeling. For example, the inverse of the employees relationship would be Person's employer "to-one" relationship.
Entities have several different kind of properties, they have attributes and relationships (and fetched properties, but that's beside the point). It sounds like Foo and Bar are two classes you created with Foo having Bar properties. Then you want assimilate those as Entities in a Core Data model.
Typically, things are done the other way around. You create Entities in the Core Data model editor. You then link the entities by creating relationships. If you add a relationship from Foo to Bar, then it is good practice to create an inverse relationship from Bar to Foo.
Create the relationship from Foo to Bar
Create the relationship from Bar to Foo
Select that last relationship and set its inverse to the first one
Now if you want Xcode to generate the matching NSManagedObject subclasses Foo and Bar, then you can select the Entities and in the Xcode Editor menu, select "Create NSManagedObject subclasses"
You'll then see you're two classes with the proper attributes to match how your model is set up.
I've authored a book on Core Data that explains all this is much deeper details.
This is two one-to-one releationship.
To make inverse just select you entity in inverse field (When you create relation).

Modeling a to-many relationship in Core Data

I asked this question earlier, but I'm missing one important thing. I have a NoteObject entity and every note, without Core Data, just has an arrayOfTags (which is an array of NSStrings). I decided to use to-many relationships to store the tags instead of an array. So I created a new "Tags" entity, and set up a to-many relationship from my NoteObject to Tags. This works great when every tag is related to only one note, but I'm unsure how I would go about linking one tag with multiple notes. How would I set up that relationship?
In you data modeler just make the Tags -> Notes relationship a to-many relationship. If you have already generated the Entity subclasses you will have to redo those. You will also have to be careful when creating tags to ensure uniqueness, but other than that it should be fairly straightforward. myTag.notes should work just as well as myNote.tags.