Avatar of AkAlan
AkAlan

asked on 

Alter MVC 3 Repository class after Scaffolding generation

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; }
           
         
        }
ASP.NET.NET ProgrammingWeb Frameworks

Avatar of undefined
Last Comment
AkAlan
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

I can't quite see the relationship for the elements, so how does LOG_OrgCodes relate to OrgCodes?
Avatar of AkAlan
AkAlan

ASKER

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.
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Let's try this...can you attach a .png screen shot of the .edmx data model?
Avatar of AkAlan
AkAlan

ASKER

Here is a screen shot of the relevant entities.
EDMX.PNG
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of AkAlan
AkAlan

ASKER

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.
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo