What is the difference between
ObjectQuery<SalesOrderHeader> query =
context.Contacts.Include("SalesOrderHeaders").Include("SalesOrderDetails");
and
Contact contact =
context.Contacts.Include("SalesOrderHeaders.SalesOrderDetails").FirstOrDefault();
Any advantage of using one over the other?
My main confusion is for the using 2 Include in first one and using 2 tables in second Include.
Thanks in advance.
On entity framework you have something called navigation properties (Named on your .edmx model) so you can access related entities based on them, in other words, you can use your navigation properties to eager load (include) related data..
I'm guessing you have this relationship: One Contact can have many SalesOrderHeaders and one SalesOrderHeader can have many SalesOrderDetails, so if you start loading from contacts entity and want to have both relations loaded you should do:
Contact contact = context.Contacts.Include("SalesOrderHeaders").Include("SalesOrderHeaders.SalesOrderDetails").FirstOrDefault();
as you can see, we're using first include to bring SalesOrderHeaders from DB and the secound one to bring SalesOrderDetails through SalesOrderHeaders navigation property as we're starting from Contacts
Related
I'm really new to Entity Framework (currently using EF5) and vs2012 and am having difficulty trying to figure something out.
I have an .edmx that was generated from my database. It has two tables in it: Item and 3rdPartyItem. In short, the Item table is the main table for all the company items while the 3rdPartyItem table is a table that is used to hold additional attributes for items. This 3rdPartyItem table was created by an outside company and is used with their software for the company, so I can't mess with either table. What I'm trying to do is show a list of Items in a grid but I need to show a combination of all the fields for both tables. In vs2012, if I create a relationship and make it 'zero-to-one' (because for each record in the Item table, there doesn't necessarily have to be one in the 3rdPartyItem table), vs complains about not being mapped correctly. When I set the mapping, it then complains that there's multiple relationships. I did some research and found that you can't create a relationship with 2 primary keys, so I was thinking that was the problem. But how can I change the .edmx so that in code, I can access Items and 3rdPartyItem like so:
var items = dbContext.Items;
items.3rdPartyItem.SomeField <--field from 3rdPartyItem table.
not too sure if it's even possible, but it would be very, very helpful if so. Any ideas?
What you're looking for is table-per-type (TPT) mapping inheritance. You can view an MSDN walkthrough here (although you'd want your base type to be instantiable):
http://msdn.microsoft.com/en-us/data/jj618293.aspx
I have 2 entities. One describes the Section of the TableView (A Month its name, etc.) This entity is related with a one to many relationship to another entity which should describe the rows of the TableView.
I'm a bit confused how to get those entites by an NSFetchedResultController. As far as I now I can only fetch one relationship at the time. So which one should I get to fill the table properly?
If you're using NSFetchedResultsController, you fetch the objects you want to display in the table view.
To get sections, you use NSFetchedResultsController's sectionNameKeyPath property to indicate how to find a section name from one of the fetched objects. This key path is something you could pass to one of the fetched objects via valueForKeyPath: to get the section name. In your case it would require traversing a relationship back to the month entity (or whatever it really is) to get its name. For example if the relationship is called month and the Month entity has a name attribute, you would pass something like #"month.name" as the sectionNameKeyPath argument when you create the fetched results controller.
You can also use the excellent Sensible TableView framework to automatically fetch the Core Data objects and display them in a table view. The framework will also detect if the entities have any relationships and will automatically manage the detail view controllers between them.
I have 2 entries
1. category (main one)
2. info
the relationship is 1 to many , for every category there is several info's
after i added the several info classes to each categoty
i want to get each category relationship list in the detailsviewcontroller
I created a temp class in the details view the contain the selected category
how do i get access to the list of INFO'S ??
Without seeing your code or model classes, I can't give you the exact answer. But when you made your model, you probably named your relationship something like "infosForCategory". When you generated the model it made a NSMutableSet for that 1-to-many relationship. In that case, you can access the list of infos with:
NSMutableSet *myInfos = myCategory.infosForCategory;
I'm not exactly sure what you're asking but it sounds like you already have an instance of Category and you want to retrieve all the related instances of Info. In that case, Category should have an automatically generated info property that is of the NSSet type. That set will contain all of the related Info objects.
Ok. So here is the deal. I have two entities - "Product" and "Parts". The product consists of parts. And parts are reusable in other products. The relation between those entities is many-to-many. And it all works great.
The problem is that I cannot add the same part to the same product twice. EF seems to force all the related entities to be unique. Consider the following code:
var product = context.Create<Product>();
var part = GetSomePart();
Console.WriteLine(product.Parts.Count); // will output 0
// Add a part
product.Parts.Add(part);
Console.WriteLine(product.Parts.Count); // will output 1
// Add the same part again
product.Parts.Add(part);
Console.WriteLine(product.Parts.Count); // will output 1!
So ok, I get the point - avoid duplicates or something. But I need this to be possible. Is there a way to do this (to tell EF to stop enforcing unique values) without creating an additional table? Or is the only way to resolve this is to manually add the intermediate table and handle the many-to-many myself?
In this case you will have to create another table called "ProductParts" which will have an identity unique key, and which can hold references to both product and part, and they can be multiple too.
In the second add statement it will not add another object because part is already in added state. So you need to create a new object with same properties add it again.
product.Parts.Add(new Part{someProperty=part.someProperty ... ect });
if you want to reduce code you can use Automapper ( http://automapper.codeplex.com/ )to copy all properties,
product.Parts.Add(Mapper.Map<Part,Part>(part));
I want to have an ObjectQuery that returns tracked entities (not static data), but I don't want it to load all the columns, I want some columns to load as null, I don't want to use select, since this will return an IEnumerable of the values, not tracked objects.
Is there a way to do it?
If yes, how do I then complete reloading those columns on demand?
Have you tried creating a view and then mapping the view?
By creating a view you can select the columns that you really want and only those will show up on the Entity Model.
I think the only way is to create new entity type which will not contain columns you don't need. You will map this entity type to the same table. On demand (lazy) loading works only for navigation properties.
Edit:
My previous idea doesn't work but in some special cases you can use idea from this article. Instead of modeling single entity from single table you will model multiple entities related with 1:1 relations. Entities will not overlap in properties (except the primary key) as my previous idea assumed because it doesn't work. You will than have main entity with fields you want to load immediately and related entities which will be lazy loaded when needed.