MSDN > 論壇首頁 > ADO.NET Entity Framework and LINQ to Entities > .Net EntityFramework binding to gridview
發問發問
 

已答覆.Net EntityFramework binding to gridview

  • 2009年7月2日 下午 06:09chekm8 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

    For simplicity I am going to use Northwind as an example... I would like to have a databound DataView that displays "Order" information data from two different entities "Products" and "Order Details"

    I want to build a web form where when the user clicks on the screen to view a "Order" It would display a single databound Dataview that contains the "Products.ProductName" and the "Order Details.Discount" for that product for the selected customer. The Discount will be editable/updateable where the Product Name will not. I know I will need template fields but what I cannot get to work is having both entitiies in the same dataview.

    I have tried several ways of databinding but none have worked.

    I will have an ADO .Net Entity Data Model that I will use that contains these (and other) tables.

    I have searched the web for an example but I must not be using the correct key words, I can't believe I am the first one looking for this functionality.

    I am hoping for a pointer to an article or example that is similar to my situation.

    Thanks in advance, -J

解答

  • 2009年7月6日 上午 07:31Diego B VegaMSFT, 版主使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆
    Hello,

    In order to be able to update the Discount property you will need to bind to a query that returns "Order Detail" entities, since Discount belongs to it. You can then still display the ProductName in read-only mode, for instance, using a template column with a binding expression such as Eval("Product.ProductName").

    However, you have to keep in mind that Entity Framework will not return the products associated with the "Order Detail" entity unless you explicitly ask for it. In this case, for an "Order Detail" entity the Product property will be null unless you instruct EF that you want it to be returned too. The way to ask for related entities to also be loaded is through the Include() method in ObjectQuery<T>.

    If you are using the EntityDataSource, you can get the same results by filling the Include property in the data source. For instance, you could set the EntitySetName to "[Order Details]" and Include to "Product". The string assigned to Include has to the name of the navigation property that connects both entities (or a navigation property path if you need to navigate several levels).

    Hope this helps,
    Diego
    This posting is provided "AS IS" with no warranties, and confers no rights.

所有回覆

  • 2009年7月6日 上午 07:31Diego B VegaMSFT, 版主使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆
    Hello,

    In order to be able to update the Discount property you will need to bind to a query that returns "Order Detail" entities, since Discount belongs to it. You can then still display the ProductName in read-only mode, for instance, using a template column with a binding expression such as Eval("Product.ProductName").

    However, you have to keep in mind that Entity Framework will not return the products associated with the "Order Detail" entity unless you explicitly ask for it. In this case, for an "Order Detail" entity the Product property will be null unless you instruct EF that you want it to be returned too. The way to ask for related entities to also be loaded is through the Include() method in ObjectQuery<T>.

    If you are using the EntityDataSource, you can get the same results by filling the Include property in the data source. For instance, you could set the EntitySetName to "[Order Details]" and Include to "Product". The string assigned to Include has to the name of the navigation property that connects both entities (or a navigation property path if you need to navigate several levels).

    Hope this helps,
    Diego
    This posting is provided "AS IS" with no warranties, and confers no rights.
  • 2009年7月17日 下午 08:27chekm8 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     包含代碼

    Diego,
    I have done a bunch more research and was able to pull from the entity object the values that I required using a LINQ query then bind them to the datagrid successfully. I used the template columns and a linq query in the codebehind.

    Is there a way I can do it so the Entitydatasource automaticallly handles the Update/Edit capabilities.  Based on what I am seeing I now need to go through and create a bunch of code to handle the rowupdating/deleting etc events.  I just want to use the Entitydatasources capabilities.

    Here is the a variation of the query I used:

    var ph = from usr in Product from pnts in Order.OrderDetail.DefaultIfEmpty()
    select new { Discount = pnts == null ? 10 : pnts.Discount, ProductName = usr.ProductName };
    



    I   Had also tried to bind the grid declaratively using the Include in the EntityDataSource and was able to get this to work, but only where a value existed for Discount, but if for some reason the discount wasnt tentered It wouldnt show the products.  Seems like and some was worng where it doesnt perform the outer join.

  • 2009年7月18日 上午 06:32Diego B VegaMSFT, 版主使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    Hello,

    Is that how your query really looks? I am not sure how it works, since I don't see a relationship between Products and Orders or OrderDetails being referenced.

    After re-reading the first post, I am not completely sure I understad the scenario. In any case, it may be useful for you to know, that EntityDataSource can only do UPDATE, INSERT and DELETE operations on a single EntitySet. Therefore, if Discount is on OrderDetails, you need to specify that in the EntitySetName property. From there, you can do as I described before: use an Eval() expression to drill down into properties of the related entities, Product, Order and Customer.

    EntityDataSource cannot use LINQ queries in .NET 3.5 SP1, but it can in .NET 4.0.

    Hope this helps,
    Diego
    This posting is provided "AS IS" with no warranties, and confers no rights.
  • 2009年7月20日 下午 03:21chekm8 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    Diego,
    Thanks again for the reply...The LINQ query I was using does look similar to the above and I gather that it works because of the joins defined in the EDMX file.  Like I said I am not using Northwind, but I am using a similar database sttructure.

    I was unaware of the limitation that you can only update the table specified in the EntitySetName property.  Also thanks for the information that LINQ queries do not apply to EntityDataSets in 3.5.

    I will make a few more attempts and keep you update on my results.

    Thanks again.
    -J