none
How to retrieve data when id is skipped or do not have id while using entity framework? RRS feed

  • Question

  • I prepare to use entity framework, however, our UI is like Access application having previous and next button

    1. When reading the following code, i got a question about How to retrieve data when id is skipped or do not have id while using entity framework?

    as currently i use

    string queryString = "Select * from (";
            queryString = queryString + "Select ROW_NUMBER() OVER(ORDER BY Project_Accrual_ID) as num, * ";
            queryString = queryString + " from dbo.Project_Accrual";
            queryString = queryString + ") as a where a.num = " + current;


    to get data

    i think that there is a need to maintain a table to map a sequence id generated externally with the id in the list

    2.  what is _repository in the following code?

    3. When there is a trigger to record the session id after insert, can savechanges be success to save without saying session id lost? as my stored procedure have to exec login again before insert statement executed

    private void RetrieveCustomersForOrders()
    {
    	for(var i=0; i<selectedOrders.Count; i++)
    	{
    		selectedOrders[i].Customer =_repository.RetrieveCustomer(selectedOrders[i].OrderId);
    	}
    }
    private Customer GetCustomerByOrder(int orderId)
    {
    	using (var ctx = new NorthwindEntities())
    	{
    		return ctx.Orders.First(o => o.OrderId == orderId).Customer;
    	}
    }

     


    Sigma : Market always changed. God bless you.


    • Edited by 沈世鈞 Monday, October 3, 2011 1:53 AM
    Monday, October 3, 2011 1:30 AM

All replies

  • Hello,

    It looks you would like to achieve the data paging. Paging at the database level was mostly the best way to optimize the performance. You can check this. http://ahmedelbaz.com/2010/05/03/entity-framework-and-linq-to-sql-paging/

    HTH,

     


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, October 4, 2011 5:30 AM
  • I haven't used extension before, where should i add this extension,

    i have created a new class file, however, no "page" can be selected after Project_Cert_TestModel.Project

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    /// <summary>
    /// Summary description for ExtensionClass
    /// </summary>
    public partial class Extension : global::System.Data.Objects.DataClasses.EntityObject
    {
        public static IQueryable<T> Page<T, TResult>(this IQueryable<T> query,
                                                     int pageNum, int pageSize,
                                                     Expression<Func<T, TResult>> orderByProperty,
                                                     bool isAscendingOrder, out int rowsCount)
        {
            if (pageSize <= 0)
                pageSize = 20;
    
            rowsCount = query.Count();
    
            if (rowsCount <= pageSize || pageNum <= 0) pageNum = 1;
    
            int excludedRows = (pageNum - 1) * pageSize;
    
            if (isAscendingOrder)
                query = query.OrderBy(orderByProperty);
            else
                query = query.OrderByDescending(orderByProperty);
    
            return query.Skip(excludedRows).Take(pageSize);
        }
    }

     


    Sigma : 2*n trials, from learning to discover
    Tuesday, October 4, 2011 9:41 AM
  • did you really need to page something? can you describe your need more detail or capture some demo screenshot here?

    Thanks,

    Werewolf,


    Just a newbie for everything.
    Wednesday, October 5, 2011 5:19 AM
  • i want to change to use entity framework and make the web more advanced and object oriented

    the UI is as follows


    Sigma : Every day has new and fresh idea and then search whether exists,
    • Edited by 沈世鈞 Thursday, October 6, 2011 1:44 AM
    Thursday, October 6, 2011 1:19 AM
  • Hello again,

    From the screenshot, you design your application to show each project record or others in each web page and click next/previous to show next/previous record. At very first, you still need to load the data from the database into your memory, then you can use linq or other technologies to query and show the data in the web page like adding the "Project Code" once when you add Next button or other equal operations. In your case, you said "If the id was skipped or null", I suggest you should force consumer to type-in something like ID or other unified identity into one record to help you make a distinguish between each data. Of course, based on my experience, we should bury a hint for this when we designed database side.

    HTH,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, October 6, 2011 5:10 AM