Background Info
So I am using Breezejs and Knockout with EF5 and the Breeze MVC api controller on the backend. One of my tables in my data base is an association table, 3 columns - an id and two foreign keys(we'll call them fkey1 and fkey2 with table1 and table2). In my application, I need to add a record to this association table. Breeze knows about the relationships that this table has.
Situation
Breeze js makes a new record for me, then I find out the records I need to associate with it. Do I need to add in the id of the table1 entity into the fkey1 observable and the table2 entity id into the fkey2 observable AND add the whole entities into their respective relationship properties AND add push this new entity object into the table1 and table2 entities association property? Or does adding the id's into the new object automatically add those objects into the relationship properties (maybe those objects are subscribed to the fkey1 and fkey2 properties? - this is what I'm guessing happens in the background of breeze, a shot in the dark though I have no idea).
Creating new entities with Breeze is super easy and I love it, but I'm a little confused when it comes to creating new entities that have a lot of relationships.
Let me if you need a better description of my situation, it's kind of a tough thing to explain. Thanks!
Providing that you set the "foreign key" properties appropriately when creating and attaching entities, Breeze will automatically update all of the associated relationships, i.e. navigation properties on this and any related entities. You should never need to manually perform any fixup.
Similarly, if you do the reverse and assign an entity to a scalar navigation property then Breeze will automatically update the foreign key(s). For a collection navigation property, if you push a value into the collection then Breeze will automatically update the foreign key of the entity being pushed.
Hope this helps, but maybe I'm missing the question...
Related
I have a core data design with multiple tables using relationships. My database is SQLite. For updates I import data from JSON and use this method:
[NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:context].
I have added unique constraints in core data.
If I update an entity that is a relationship of another object it loses the connection.
Ex: Entity "person" that contains the one to one relationship to "pet_id". If I update "pet" it changes his id and "person" still points to the old id, so they are not related any more.
Is there a way to avoid this problem?
I don't think this is documented anywhere yet. Here's what it sounds like is happening:
You create a new instance. Your constraints mean that this instance matches an existing instance. But...
Your new instance has a nil value for this relationship. So...
The existing instance's value for the relationship is replaced by this new nil value.
To maintain the relationship, your new instance needs to already have the correct value for that relationship. You're essentially asking that the constraint matching system ignore the fact that the relationship value is different in your new instance, but to accept new values for other attributes.
I think what you're expecting is completely reasonable but I'm also not surprised that the current implementation doesn't support it. I recommend filing a bug with Apple about this, and investigating non-constraint based approaches to keeping your data unique.
I am using coredata to save the server data through web services in my application and I am storing relationships as an object to the entity.
I have many entities e.g "Inspirations" and "Products" and both are related to each other. I have a problem whenever the records are updated in the third entity which is "Filters" then the relations of the entities broke and I cannot apply filters on the entities.
[object addRelatedInspirationsObject:related];
This is how I save relationships. I am not able to figure out why the relations are being broken once the entity is updated which has no direct link with the entity.
One thing more if I fetch and save the data of any one of the entities like "Inspirations" then all the relations start to work again.
Your code should work. Here are 2 things you need to check:
Make sure related is not nil when you call your method.
Make sure you call save on a valid managed object context.
From your question it seems that entities have 1 to many relationship between them. And by the code you supplied, every things should work fine. Just make sure, you are using the Filter object from the relationship like object.filter (or obj1.obj2.filter), not accessing it via a direct NSPredicate on Filter entity and updating it. And if you are using FRC, you might also need to generate a fault against the parent entities, to get your UI updates.
I have an entity (Org) in my Entity Framework that has a foreign key with a table that is in another database (BusinessUnit). We need the foreign key to get the description of the BusinessUnit linked with the Org. In the past (old project without Entity Framework) we were using a stored procedure to return all the information about this entity, including the BusinessUnit description, using a join. So now my problem is how to display the same information than before using Entity Framework.
I've tried, once I load my Org entity from database, to make a loop accessing to BusinessUnit to get the description for each Org, but this is too slow. My other idea was use a store procedure, but I need an extra field on my entity and Entity Frameworks gives me an 3004 error: No mapping specified for my property. I was thinking to use a complex type, but I'm not sure if it's what I need keeping in mind I have to add just a field to my entity. In that case, could I use the complex type just for "select" operations and keep my original entity for the rest of CRUD operations?
How should I proceed?
Thanks.
EF is not able to execute queries across multiple databases. If you need to perform such query you must either use database view and map the view as a new entity (it will be readonly - making it updatable requires mapping insert, update and delete stored procedures) or divide your data querying into two separate parts to load data from both databases. Divided querying can either use two contexts or you can get data from the second database using stored procedure.
The reason why you got the error is that you added the property in EDMX. EDMX can contain only properties mapped to your first database. EDMX generates entity code as partial classes If you need property manually populated from the second database you have to create your partial part (partial class) for entity and add the property in code.
I have 2 entities (say People and Books) that have two many-to-many relationships. I have created two different linking tables - e.g. the linking tables are called BooksCheckedOutByPeople and BooksOnHoldByPeople.
EF 4.0 correctly makes two relationships. It calls them something like PeopleBooks and PeopleBooks1.
When I am making Linq queries, how do I tell Linq to use a specific one of these relationships? Is there any way in Linq to specify one relationship instead of the other?
Say I'm creating a query against People and I want to get the Books for BooksCheckedOutByPeople and thus I need to use the relationship PeopleBooks.
Thanks.
You should be able to rename "PeopleBooks" and "PeopleBooks1" to more informative property names by editing the model EF generates for you. Something like "BooksOnHold" and "BooksCheckedOut".
At that point, when writing your LINQ queries, just reference the right navigation properties (as they're called). LINQ uses whichever properties you specify, and the Entity Framework should generate a unique navigation property for each collection.
Edit
I just fired up VS2010 to copy your model and poke around a bit.
I see that EF4 did indeed generate two Navigation Properties foor Book and Person, called People and People1, and Books and Books1 (respectively).
If you select any of these Navigation Properties in the Model Browser and look at the Properties pane, you should be able to see which table is correlated to that association and rename the property appropriately. Here's a screenshot from my PC:
You can see that I've selected the "People" nav property for the "Book" entity. The association in this case is determined by BooksCheckedOutByPeople, so I can rename the property to "PeopleCheckingOut", or something more useful than "People". When I'm using LINQ-to-Entities later, I then reference the "PeopleCheckingOut" property to query that collection on any specific Book.
Consider an application dealing with houses and pictures. An entity named House has a 1:n relationship to HousePicture.
The application allows the user to create and edit a House, and add/remove HousePictures.
[HttpPost]
public ActionMethod Edit(House h)
{
//get an attached entity from the DB first.
House original = db.Houses.SingleOrDefault(x=>x.ID==h.ID);
UpdateModel(original);
//collect all the uploaded pictures.
original.HousePictures = GatherPicturesFromForm();
db.SaveChanges();
// the changes for the House are saved,
//but child collection Pictures are not.
}
How do you go about updating - adding new & deleting - the child collection when recreating the child collection from scratch?
Add() or Attach() for each child in the collection?
In what sequence do you need to Add or Attach the parent entity vs. the child collection?
How to go about detecting the children to remove? Is this a feature of EF4 where deletes happen automatically by the framework, or does the developer need to write this logic?
When adding more than one HousePicture, its ID == 0. The entity has the primary key in SQL Server has an auto-assigned PK of int IDENTITY(1,1). This becomes a problem because EF4 thinks that 2+ child have the same ID.
What are your recommendations on saving child collections using Entity Framework 4?
Any other suggestions on making the persistence of 1:n collections easier when updating/adding/deleting?
In EF just add new objects to a collection and they'll automatically be persisted to the database with the correct foreign key values.
original.HousePictures.AddRange(GatherPicturesFromForm());