Calling on a property of an entity's parent in a relationship - xcode

I have an NSColumnItem Selected Value bound to a relationship property (jobParent) in an entity (jobs). This is an inverse relationship to a property (projectChild) in a separate entity (projects).
How do I call on the projectTitle property of my projects entity to display in my NSTableColumn, based on the relationship to the currently selected jobTitle (from my jobs entity) in my NSTableView? I tried entering jobParent.projectTitle for the Model Key Path and arrangedObjects for the Controller Key but it's not working (as well as any other combination I can think of). Thanks.

The answer is to make sure the related entity is bound to 'content set' and then address the properties of the related table by using a period after the relationship property of the child entity followed by the property you want of the parent entity. For my example above, to get the projectTitle property of my projects entity in relation to my jobs entity, I would call on:
jobParent.projectTitle

Related

Many to many checkbox and mvc

I'm using Entity Framework 4 and my model relationships are automatically generated from the lookup table. My models consist of Request and Building. A request can have many buildings, and a building can be associated with many requests. I've found a few posts on how DropDownFor automatically selects an item based on model relationships. But the HtmlHelper CheckBoxFor wants an expression that returns bool. My models don't have a bool indicating checked because it is based on the relationship.
Anyone have tips or experience?
Don't pass your EF models to the view. Define view models which are classes specifically tailored to the needs of a given view. You don't need many-to-many recursive relations in the view. So in the case where you want to generate a checkbox you would have a corresponding boolean property on your view model. It's the controller that would query the repository, fetch the EF models, map them to the view model (this task could be simplified with frameworks such as AutoMapper) and finally pass the view model to the view so that in your view you simply:
#Html.CheckBoxFor(x => x.SomeBooleanProperty)
And if you wanted to have a list of checkboxes then your view model would contain a collection property of some type that will hold the boolean property.

Implementing identifying relationships with EF4

I'm currently in a situation where I need to delete entities without having access to the associated ObjectContext. I read about identifying relationships and they seem to be exactly what I need: I want to delete an object, once it is no longer referenced by its "parent" object.
I am using Visual Studio 2010 Premium to generate my database from an edmx file. As far as I understand, I need to include the foreign key of my "parent" object in the primary key of my "child" object table. However, I cannot find a way to tell Visual Studio to do this.
Can someone please help me out on this? Am I completely on a wrong path or am I just missing a setting somewhere?
I finally figured it out:
Go to your Child entity and create a scalar property ParentId. Set this property as entity key (making it a primary key, together with your Id property of your Child entity). Next go to your ParentChild relationship and add a referential constraint. Principal for the constraint is your Parent and Dependant is your Child. Dependant property must be the property you just created on your Child (i.e. ParentId). Save everything and you're good to go.
Basically this is described as "scenario 2" in this blog post: http://mocella.blogspot.com/2010/01/entity-framework-v4-object-graph.html
No, you are in the right path. What you need to do is in the EDM designer, after creating your 2 entities (Parent and Child), right click on the Parent Entity and select Add => Association... and then specify Multiplicity and Navigation property names, and click Ok. You'll see that VS create an association in between which will result on a relationship between these 2 table later on when you generate a database from your model.
Do not create a property like ParentID on your Child entity as it will be automatically created by the designer once you create the association.
Furthermore, you can right click on the association in the EDM designer and Select Properties and Select "Cascade" on "End2 OnDelete" option so that the child will be deleted when the parent is deleted.

How do I add a navigation property for a Entity Framework Complex Type

I'm using VS2010 Beta 2, I have a Complex Type called Address with the following properties:
Street
City
CountryId
I have a Country Entity defined in my Model, but I can't seem to find a way to add a reference (Navigation Property) from the CountryId property of my Complex Type to the Id property of my Country entity.
I'm I going about this the wrong way or is this something that I can't do with the designer??...
Another option I have is just creating an Address entity, but it just doesn't sound right to me.
No, you can't do this, because it goes against the idea of what complex types and navigation properties are. Complex types have value semantics, rather than reference identity. Navigation properties are first-class entities and have reference semantics. Therefore, they cannot be part of a complex type. As it seems you've discovered, the solution is to partition the parts with value semantics into the complex type, and add it to an entity containing the navigation properties you require.
Ok,
It seems like the current version of the Entity Framework doesn't support a Navigation Property or foreign key within a Complex Type.
The other sensible option is to create a separate Address Entity, which will have all of the foreign keys it needs, and then create Navigation Properties within all my other entities.

Tinkering under the hood with Bindings in Xcode

Let's take two Core Data entities, set up as follows:
Entity A: Car
Attributes:
year
model
Relationships:
manufacturer (<<-> Manufacturer)
Entity B: Manufacturer
Attributes:
name
country
Relationships:
cars (<->> Car)
Now, what I want to do is bind the display to an NSTableView where we have the model of the car in one column, followed by the manufacturer, followed by the year. Binding the model and year are no problem, but if I bind the relationship to a column in the table, I get the text of a relationship fault error in each cell in that column instead of anything I'm looking for. How can I play with the binding to allow me to display the proper manufacturer name associated with the car?
Extending the question a bit further, how could I set up another table view to display, say, other Car entries with the same manufacturer relationship?
A bit more information about how you have it set up currently would be helpful. You should be able to bind to your Array Controller in exactly the same fashion as your other attributes, with the same binding and controller key. Just use the full key path manufacturer.name for the model key path.
For a to-many relationship, you use two array controllers. Set up the 'master' array controller to prepare its own content from your Core Data Manufacturer class (in Entity mode). Then, you create a secondary, 'detail' array controller. Leave the detail array controller in Class mode (with the default NSMutableDictionary class), and bind its content set to your master array controller, with the controller key set to selection and the model key path to cars.
Many many tutorials exist out there that do exactly this. I highly recommend running through one or two; I found this MacResearch.org tutorial particularly helpful. The entire series is great.

What does "Controller Key" mean in Interface Builder > Inspector > Bindings?

I can't find in the Docs where they explain all those fields and what they mean. Especially "Controller Key" is not clear to me.
[Copying my answer on another question…]
The controller key is the key for the (property of the controller object) you're binding to. The model key path is the key path by which the bound object can ask the model objects for more basic objects, such as strings or images, or for other model objects (i.e., drill down into the model).
An example: Let's say you have a Person objects in an array controller, and each Person has a name. You bind a table column to the array controller, controller key arrangedObjects (thereby getting the model objects), model key path name (thereby getting the value objects).
A more complex example: Suppose you have an array controller of Departments. Each Department contains Persons (employees in the department). You can bind your People array controller to the Departments controller, controller key arrangedObjects (getting the Department model objects), model key path #distinctUnionOfObjects.employees (getting the Person model objects), and then bind a table column to the People controller, controller key arrangedObjects, model key path name.
That table would be for people who work for your company; if you have a separate table of prospective employees, you can create Person objects for them, too, and they won't show up in the table of existing employees because they're not in a Department. When you hire them, you'll add them to one or more Departments; then, they'll show up in the People array controller automatically, because that array controller is observing the employees of all of the Departments.
The Controller Key pop-up menu is a way to help you discover what keys the controller (typically a NSArrayController, NSObjectController or a NSTreeController) presents.
The best example is the selection key of NSArrayControllers, which contains the set of selected objects. What is confusing is the NSObjectController presents a 'selection' key too, although the controller can control only a single object (therefore the selection = the object).
I agree that it is not clear at all. I personnally began to understand it when I bound my objects programmatically (i.e. using the bind:toObject:withKeyPath:options: method).
It has to do with key-value coding. You can bind a control in IB to a value in your controller. To connect that value, you have to specify the keypath to it. For example, if you have a textfield in IB and you want to bind it to say a field called 'name' in your controller, you would specify 'name' as the keypath. You then need to set up your name field in your controller to be accessible through key-value coding. This is done in 10.5 by using the #property and #synthesize specifiers.
Take a look at this topic: Cocoa Key Value Bindings: What are the explanations of the various options for Controller Key?
I posted an explanation of where to find definitions for all Controller Key's there.

Resources