EF Add filter to navigation property DbSet - entity-framework-6

I want to add .Where(e => e.PartitionId = x) to all entities in Linq to Entity expression.
User could send complex query which translates into multiple navigation property, for example
Order and its customers and customer's address.
I tried capturing DbSet access but it appears to only happen for Order entity and does not happen for any of navigation property.
I can't hard code query in my code because user could be asking for Order and get its navigation property or start from any of my 20 entities.

I found EntityFramework.DynamicFilter and it is working well so far

Related

Breeze: how to prevent merge to happen

Having an entity in cache, that entity has related entities in the form of a top 20.
Now a user action can update the top 20 on the server, and I thus would like to redownload the entire entity. Server sends the correct data with top 20, but in Breeze, I end up with a top 40... And I can't figure out how to avoid this behavior.
Thanks for the tip
Update: I do not use odata webapi and iqueryable, as it offers too much power to clients for my app. So I don't want to use EntityQuery.fromEntityKey, which seems to do what I want. I'd prefer to keep using a "normal" query, to which I add a parameter.
Update 2: To add more clarity as to why I want to prevent merge, when I recompute the top 20, I delete all related entries in the db and recreate them, so they have new Id's. So I am now considering an update, which might actually solve my issue BUT I would still like to know if merge can be prevented.
The Breeze EntityManager caches entities by primary key. So presumably your 2nd query is returning a completely new set of entities with each query. If this is the case, and you really only want the "latest" 20, the simplest fix would be to simply empty the EntityManager cache for this entity type before each query. Something like:
var entities = myEntityManager.getEntities(myEntityType);
entities.forEach(function(e) {
myEntityManager.detachEntity(e);
// or
// e.entityAspect.setDetached();
});
I am not sure what is exactly your qustion, but if you want to have different results on entity queries you may do it by creating 2 different controllers with different action names, or just with different parameters - overloaded methods. Then in your datacontext you can have 2 different queries.
You use .withParameters() propriety on the query to call the controller method with parameters.
Then in the controller method you can query and filter with LINQ, in any way you want. This way you can have different results based on the query/controller you chose to call.
Documentation: http://www.breezejs.com/documentation/querying-depth

breezejs: orderBy not kept when merging collection to an existing Entity in cache

In the cache, I've got an Entity of type 'Mandate'.
Then I run the following code to fetch a collection of MandateHistory entities, which is then merged by breeze to the corresponding property of the Mandate entity :
function getMandatHistory(mandatId) {
var query = breeze.EntityQuery.from("MandatesHistory")
.where("Mandate.Id", "==", mandatId).orderBy("Id")
.expand("Mandate").skip(offset).take(pageSize).inlineCount(true);
return manager.executeQuery(query.using(service));
}
Note the orderBy clause is respected and the results are properly sorted by Id.
However the items in the collection property of the Mandate entity is NOT sorted. Do I have to do something special here ?
Sorting of the values returnd by collection navigation properties is NOT something that Breeze does. It will sort the results of a query, but if you want to sort, ( and keep sorted), one of the collection properties of an entity you will need to manage that yourself.
I think your best two options are either.
1) Sort before display. i.e. call a sort method on any collection right before you display it. Depending on what MVVM framework you are using, there is often a 'binding' that does exactly this.
2) Subscribe to the Breeze arrayChanged event on the array returned by your navigation property and call sort on the array anytime you see the change event. Note this can get expensive if you subscribe to a lot properties on a lot of entities.

Adding a record into an association table using Breezejs

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...

Performance implications : Entity table reference vs List of that table reference

I am using entity framework and developing an architecture for application with remote data access. Coming back to point, i query the database for one record (say on the basis of itemcode). Now the resultset i will get whether i should return it as List or collection or simple as an object of entity. I am using entity object but my boss is saying i should use List. He thought , returning result as an entity with return whole table structure also. Quick suggestion would be appreciated.
List<Employee> lstemployee = GetRecordByCode(itemCode)
or
Employee emp = GetRecordByCode(itemCode)
What's the difference? If itemCode is a unique key you will either get one Employee object or a list containing the same one Employee object. You will never return the whole table. That will only happen if within GetRecordByCode you do something like context.Employees.ToList() without any Where filter before the ToList().
If itemCode is not unique you even have to use a list.

Entity Framework 4.0 2 many-to-many with same entities

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.

Resources