none
How to use EF layzy loading? RRS feed

  • Question

  • Hi all, 

    I want to using EF layzy loading to get data from database, which I am using Code first. 

    The code I used from this site: http://www.asp.net/web-api/overview/data/using-web-api-with-entity-framework/part-4

    The issue I met is that I can't get the data of virtual marked. 

    I tried few solutions, unlucky, All failed.

    Can anybody help me out?

     


    Life is not so easy.......

    Tuesday, July 14, 2015 10:21 AM

Answers

  • I am already know how to use it and know why it doesn't work. 

    To share the solution, I will show my email address here to provide the details. 

    I will mark this post as an answer to help the moderator to close it. 

    Email :Email Address


    Life is not so easy.......

    • Marked as answer by QLoveQ Wednesday, July 29, 2015 3:07 AM
    Wednesday, July 29, 2015 3:07 AM

All replies

  • http://www.asp.net/web-api/overview/data/using-web-api-with-entity-framework/part-4

    The link above and its example seems pretty straight forward. Books are in a collection and each Book has an  Author. You read a Book object and load the Author object in the Book object related to the Author.

    Tuesday, July 14, 2015 2:59 PM
  • If the book has 1 author, it's OK. 

    But if the book has more author, something like this: 

    public  virtual ICollection<Author> Author { get; set; }

    The Author is always empty. 

    Not sure why.


    Life is not so easy.......

    Wednesday, July 15, 2015 5:33 AM
  • If the book has 1 author, it's OK. 

    But if the book has more author, something like this: 

    public  virtual ICollection<Author> Author { get; set; }

    The Author is always empty. 

    Not sure why.


    Life is not so easy.......

    If you are not using eager loading using and Include in the Linq query, then you may have to load the collection manually. Also I noticed that WebAPI that you are using which shows using EF at the controller that is nothing but pure trouble. Also, you don't use lazy loading in a Web application, which is too many trips back to the database.
    Wednesday, July 15, 2015 11:17 AM
  • Hard to understand what's your meaning. 


    Life is not so easy.......

    Thursday, July 16, 2015 2:58 AM
  • Hard to understand what's your meaning. 


    It's not that hard, and Bing and Google are your friends.

    Explicitly Loading

    https://msdn.microsoft.com/en-us/data/jj574232.aspx

    --------------------------------------------------------------------

    Other stuff....

    https://en.wikipedia.org/wiki/Separation_of_concerns

    <copied>

    Layered designs in information systems are another embodiment of separation of concerns (e.g., presentation layer, business logic layer, data access layer, persistence layer).

    <end>

    https://en.wikipedia.org/wiki/Multitier_architecture

    https://msdn.microsoft.com/en-us/library/bb384398.aspx?f=255&MSPPError=-2147217396

    http://www.codeproject.com/Articles/70061/Architecture-Guide-ASP-NET-MVC-Framework-N-tier-En

    The bottom line is don't get yourself burnt using EF in the MVC or WebAPI Controllers and learn SoC.

    Below is a WebAPI Controller that is being using in a N-Tier MVC solution. No controller being used in MVC or WebAPI controller is using the database directly. The DAL is  in contact with the Database, which you don't see, and not the Controllers in direct contact with the database. No program in the solution using a controller MVC or WebAPI is accessing the database directly. 

    So don't sip the MS Kool-Aid too hard and start directly accessing a database through a controller using and ORM or not is the message, because you will get burnt. Learn SoC to circumvent it.


        public class InboxController : ApiController
        {
            private readonly IMessageServiceClient _messageserviceclient;
            private readonly IEFilePrincipalContextService _principalContextService;

            public InboxController(IMessageServiceClient messageServiceClient, IEFilePrincipalContextService principalContextService)
            {
                _messageserviceclient = messageServiceClient;
                _principalContextService = principalContextService;
            }

            public dynamic Get([ModelBinder(typeof(Models.DataSourceRequestModelBinder))] DataSourceRequest request)
            {
                var list = _messageserviceclient.GetInboxMessages(request.Page, request.PageSize, EFilePrincipalHelper.GetUserId());
            
                var vmList = new List<InboxMessageModel>();
                if (list != null)
                {
                    vmList.AddRange(list.Select(item => new InboxMessageModel
                    {
                        MessageId = item.Id,
                        From = item.FromUser.Metadata["PrincipalName"] as string,
                        To = _principalContextService.GetUsername(),
                        Subject = item.Subject,
                        Body = item.Body,
                        Read = item.IsRead,
                        ReadTime = item.ReadTime,
                        SentTime = item.SentTime,
                        CanRespond = (Guid) item.FromUser.Id != SystemUser.Id
                    }));
                }

                return new { TotalRecords =  _messageserviceclient.GetInboxMessagesCount(EFilePrincipalHelper.GetUserId()), Records = vmList };

            }
        }

    Thursday, July 16, 2015 2:13 PM
  • Nothing help. 


    Life is not so easy.......

    Friday, July 17, 2015 3:31 AM
  • Nothing help. 


    Life is not so easy.......

    Well, you don't use lazy loading in a Web based program anyway.  You get the parent object and its child objects in one shot or one call to the database using the Include function in a Linq query and eager loading.  You don't get the parent object, and then you turn around using lazy loading and start reading child objects from the database belonging to the parent. Those are too many calls to the database in a Web based solution. One call to get the parent, and then multiple calls back to the database to get the children? It too much unnecessary traffic in a Web based solution, and it's a poorly designed  Web based solution.  
    • Marked as answer by Fred BaoModerator Wednesday, July 29, 2015 2:56 AM
    • Unmarked as answer by QLoveQ Wednesday, July 29, 2015 2:58 AM
    Friday, July 17, 2015 1:36 PM
  • Do you see the "Nothing help"? 

    Don't tell me you need to close this thread. 


    Life is not so easy.......

    Wednesday, July 29, 2015 3:01 AM
  • I am already know how to use it and know why it doesn't work. 

    To share the solution, I will show my email address here to provide the details. 

    I will mark this post as an answer to help the moderator to close it. 

    Email :Email Address


    Life is not so easy.......

    • Marked as answer by QLoveQ Wednesday, July 29, 2015 3:07 AM
    Wednesday, July 29, 2015 3:07 AM
  • Do you see the "Nothing help"? 

    Don't tell me you need to close this thread. 


    Life is not so easy.......


     You can't be helped, just like blood can't be squeezed from a turnip.  This entire thread should be deleted not closed. :)
    Wednesday, July 29, 2015 3:08 AM
  • I am already know how to use it and know why it doesn't work. 

    To share the solution, I will show my email address here to provide the details. 

    I will mark this post as an answer to help the moderator to close it. 

    Email :Email Address


    Life is not so easy.......


    I think I'll pass............... :)
    Wednesday, July 29, 2015 3:13 AM