Alin I
Alin I

Reputation: 590

Entity Framework 6 Linq results for master-detail

I'm trying to create a master-detail model using the following tables:

Orders
OrdID   DATE            CustID
========================================
1       01.01.2014      1
2       02.02.2014      2
3       03.03.2014      2
4       01.01.2014      4

OrderDetails
ID      OrdID   ProdID  Quantity
========================================
1       1       1       2
2       1       2       1
3       2       3       3
4       2       4       5

Now I want to have a Orders DataGridView and a OrderDetails Datagridview.
Orders DGV should look like this:

Orders
OrdID   DATE            Customer    Status
============================================
1       01.01.2014      CustOne     100%
2       02.02.2014      CustTwo     62.5%

And Details DGV like this (filtered on selected order):

OrderDetails
ID      Prod        Quantity    Status
======================================
1       ProdOne     2       100%
2       ProdTwo     1       100%
3       ProdThree   3       0%
4       ProdFour    5       100%

Lets say that the Status Field in OrderDetail is Calculated based on data from other 3-4 tables. These tables are shown on the form, and they contain some product/order related details (production status, suppliers, etc.).
I did all this using Dataset and Datatables getting all tables from the database into a dataset, then making all the calculations using Linq results for populating other tables that had necessary relations between them, so I could bind them to datagridviews using bindingsources for master-detail.
Now i'm trying to achive this using EF. My goal would be to minimize data fetched from the database, and perform all the calculations in memory, rather than in the database.
Is there a way to create an model in EF, that would contain the filed that I need, and the data coming from the context, not from the database.(I'm pulling the Products Table, the Clients table, the OrdersTable, the OrderDetails table, etc. from the database into the Context. Tha table behind Orders DataGridView is created using joins from all these tables. I dont want to get the data in this table from the database using StoredProcedures, I want to get it using Linq from with the tables already in context).

THE QUESTIONs ARE:
I have entity:

Order with properties: OrdID, DATE, CustID
    and navigation properties: Customer, Orders.

and entity:

OrderDetail with properties: ID, OrdID, ProdID, Quantity
    and navigation properties: Order, Product.

How do I add a property to an entity, and this property is calculated, kind of like Expression in a datatable. How do I display the Orders collection on a datagridview, showing Customer info on every row.

Upvotes: 0

Views: 771

Answers (1)

LongArm
LongArm

Reputation: 208

Whilst there is a very long way of answering your question, I believe there is an alternative to what you are trying to achieve.

For the calculated property, you need to create a Partial Class with the calculated property, but you would need to ensure that the navigation property is populated first:

'pre populate data and include any additional navigated items

Dim Query = DataContext.Orders.Include("OrderDetails").Where(Function(t) t.OrderID = x)

'create a new partial class on the OrderDetails entity then create a property to calculate status:

Public Partial Class OrderDetails
    Public ReadOnly Property Status As String
        Return = (100 / Me.Quantity) & "%"
    End Property
End Class

Ideally you should create a View Model for each datagrid, then as you populate it you can create your functional / calculational properties specific to the datagrid there, but by creating the partial classes on your Model you can re-use it in any application.

Upvotes: 1

Related Questions