EntityFramework 5 - Change Initial Order of Items in Collection (NavigationProperty) - entity-framework

is it possible to change the item's order in a collection that is a navigationProperty?
sample:
A Entity "Order" has many "Item" Entities. One to Many Relation.
i want that all items of an order a sorted by the name. How could i do that without a query on the DbSet? I want that the items are sorted after i load them into the context.
Is this possible to manipulate the generated SQL Query from the EF to add a ORDER BY ?
Thx for the help!

No. You'll have to do that when you process the items in any way, e.g. for display.

Related

jpa query to update many to one association

Is possible to create a query to update a field that is part of a relationship? I wont update the childs, i just want to update the field into the association....
I tried but I get the error: can not navigate association field
How could I do this?
thank you very much
You do not need a query.
Get the object that represents the relationship, modify the values that you need and then persist it.

How to use DBContext.Add/Attach (using EF CodeFirst 4.1) with nested opbjects

Problem: When adding an object "Order" to my dbcontext, all nested objects of the order gets "readded" to the database, though the nested objects is static data and only a reference shoudl be added in the database.
Example:
The database holds 0 orders, and 3 items.
I add one order with 2 items.
Now the database hold 1 order, and 5 items. The two items in the order has been "readded" to the database, even though the items had the right primary keys before db.SaveChanges().
I realize that i may be able to attach the existing items to the dbcontext before saving changes, but is that really the only way to go? Can't EF figure out that to item already exists when the primary key matches an existing item?
Does anyone know if this is different in the new version of EF CodeFirst?
No EF cannot figure if entities are existing one or new one - both Add and Attach commands are graph oriented operations. You call them on one entity in the graph and they traverse all relations (and their relations and so on) and perform the operation for them as well.
You must figure correct state of each entity in the graph for example by using:
dbContext.Orders.Add(newOrder);
foreach(var item in newOrder.Items) {
dbContext.Entry(item).State = EntityState.Unchanged;
}
dbContext.SaveChanges();
You can use the reverse operation by calling Attach(newOrder) and set the order to Added state. The main difference will come with independent associations (for example many-to-many relations). The first approach will correctly add new relation between order and each item whereas second will not unless you manually set each relation to Added state (and changing state for relations is more complex).

Many to many relationship with ADO.NET Entity Data Model

I've created many-to-many relationship with ADO.NET with extra order fields in the middle table.
So I have...
Customers
-customer_id
-customer_name
Orders
-order_id
Customers_to_Orders
-customer_id
-order_id
-seq
And now I don't really know how to add new orders to customers with specyfing order, any suggestions?
Create the order first.
Get the ID back for the order.
Then, create the link from the customer to the order.
you add the order to orders table first, and then add it to customer_to_orders preferably in one transaction.
if you're worried about the seq - it can be either identity or you can calculate "next seq" by querying customers_to_orders before adding new data.

Can't insert entries of a many-to-many relationship in Entity Framework in a specific order

I have a many-to-many relationship between 2 entities in Entity Framework, like here . So, Employees and Projects. At one point, I would like to insert some Projects to a Employees entity in a specific order. By conserving the order I would like to know which was the first preference of the Employees for a Projects entity. The thing is that although I order the Student.Projectslist in the way I like before the insert, when selecting Employees.Projects.FirstOrDefault(), the entities are ordered after the ProjectsId and I don't get the first element I inserted. How can I conserve the order I want?
O course, I could make a new field PreferredProjects and save the other Projects in a random order, since only the preferred one is important for me. But this is not an option, being given the context of the current project's software design.
Thank you in advance...
It sounds like you simply want to have sorted child collection results when you do a query, rather than take full control of the insert order.
You can achieve that using the techniques described in Tip 1 of my tips series.
Hope this helps.
Alex
Program Manager Entity Framework Team.
Unfortunately, there is no easy solution. I have the same problem. The only solution is to save after adding each child item (project). This is the only way to save the order without using a new field column to sort input.
Try Employees.Projects.OrderBy(x => x).FirstOrDefault()

ASP.NET Dynamic Data : How to Specify Sort Order of Items in Dropdown List

I am using ASP.NET dynamic data for the data adminstration tasks for a Silverlight app that I built. It saved a ton of time not having to write all of the admin screens you typically have to create for end users to manage the data.
One thing I cannot figure out how to sort the items in the drop downs that appear on the screens - either the filter dropdowns on the list views or on the data entry screens.
Do I specify that somewhere in the EDM partial classes or in the ASP.NET DD field templates? or somewhere else?
All I need to do is sort alphabetically by the display value- they appear to be in random order.
thanks
Michael
Use the DisplayColumn Attribute in the System.ComponentModel.DataAnnotations Namespace.
http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.displaycolumnattribute.aspx
ex:
[DisplayColumn("LastName", "LastName")]
public partial class Employee
{
}
The answer to your question can be found here, about halfway down the page:
http://csharpbits.notaclue.net/2008/08/dynamic-data-and-field-templates-second.html
In the Cascase.ascx.cd FilterControl and Cascade_Edit.ascx.cs FieldTemplate you will find a method GetChildListFilteredByParent. This returns the values for the filtered DropDownList, but as you will see this list is an unordered list. To add sorting to this list we need to add a Linq OrderBy clause.