Alter MVC 3  Repository class after Scaffolding generation

AkAlan
AkAlan used Ask the Experts™
on
I have a model class that is used as a lookup for another model class (Log_ReqHeader).


Open in new window

public partial class LOG_OrgCodes
    {
        public LOG_OrgCodes()
        {
            this.LOG_ReqHeader = new HashSet<LOG_ReqHeader>();
        }
   
        public string ID { get; set; }
        public string OrgDesc { get; set; }
        public string SiteCode { get; set; }
        public bool Obsolete { get; set; }

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



After generating the view, controller and repository I end up with a method "All". this method is returning all the OrgCodes along with the collection of all the Log_ReqHeader information which I don't want.  I want to create a new method to return only the OrgCode information but am hitting a wall. I copied the All method and added an interface to it but now I am not sure of the syntax to select only the OrgCodes without the Log_ReqHeader data. Thanks in advance for any help.


Open in new window

public IQueryable<LOG_OrgCodes> AllOrgCodes
        {

            get { return context.LOG_OrgCodes; }
           
         
        }
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2008

Commented:
I can't quite see the relationship for the elements, so how does LOG_OrgCodes relate to OrgCodes?

Author

Commented:
Hi, sorry, I should have spelled everything out. The model class Log_OrgCodes contains the OrgCodes I use in Log_ReqHeader. I had changed the PK field from OrgCode to ID in the edmx so MVCScaffolding would work. This is basically a Logistics Requisitions database and each Requisition has a Header record and one of the fields is for the OrgCode and that is the purpose of the Log_OrgCodes table, to provide a list of OrgCodes in a dropdown. After scaffolding the Log_OrgCode entity I ended up with the class I showed above. Along with the model class I had a repository created also. In the repository there are several methods for returning records, one of them looks like this:

Open in new window

public IQueryable<LOG_OrgCodes> All
        {
            get { return context.LOG_OrgCodes; }
        }
When I navigate to a View that uses this method, I see that the View is not only returning the Log_OrgCodes but for every related record in Log_ReqHeader it is returning those records as well. Even worse for every Log_ReqHeader record there are LineItems and Comments from two other related entities and that data is being passed into the view as well. I only want the Log_OrgCode records. After doing some research I found one solution but I don't like it. I added a method to the repository, called it AllOrgCodes and now I can use it to only get Log_OrgCode records. It looks like this:

Open in new window

public IEnumerable<LOG_OrgCodes> AllOrgCodes()
        {
            var query = from o in context.LOG_OrgCodes
                        select new
                        {
                            ID = o.ID,
                            OrgDesc = o.OrgDesc,
                            Obsolete = o.Obsolete,
                            SiteCode = o.SiteCode
                        };
                        var OrgCodes = query.ToList().Select(o => new LOG_OrgCodes{ID =o.ID,
                                                                                   OrgDesc = o.OrgDesc,
                                                                                   Obsolete = o.Obsolete,
                                                                                   SiteCode = o.SiteCode
                                                                                   }).ToList();

                        return OrgCodes;

        }

Seems like there must be a better appraoch to this. I'm learning MVC, thanks for any assistance.
Most Valuable Expert 2012
Top Expert 2008

Commented:
Let's try this...can you attach a .png screen shot of the .edmx data model?
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Author

Commented:
Here is a screen shot of the relevant entities.
EDMX.PNG
Most Valuable Expert 2012
Top Expert 2008
Commented:
OK, let me see if I understand...it looks like you are not using lazy loading with the data context, and loading all the related data, even though you don't really need it.

Entity Framework 4.1: Deep Fetch vs Lazy Load (3)
http://vincentlauzon.wordpress.com/2011/04/11/entity-framework-4-1-deep-fetch-vs-lazy-load-3/

public MyDomainContext() 
{ 
    this.Configuration.LazyLoadingEnabled = false; 
}

Open in new window

Author

Commented:
Ok, I think I see what happened. I was using lazy loading but the Index view that got created by MVCScaffolding created a textbox to display the count of Log_ReqHeaders which then triggered the loading of all the Log_ReqHeaders. I removed the textbox and the page loads very fast now. I need to debug to make sure but it went from a 10 second load to under one second so I think that is what happened. If you have a differing thought, I would like to hear it. Thanks for your time and the link was very useful in helping me learn how this works. Now on to the next problem.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial