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

8/22/2022 - Mon
Bob Learned

I can't quite see the relationship for the elements, so how does LOG_OrgCodes relate to OrgCodes?
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.
Bob Learned

Let's try this...can you attach a .png screen shot of the .edmx data model?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
AkAlan

ASKER
Here is a screen shot of the relevant entities.
EDMX.PNG
ASKER CERTIFIED SOLUTION
Bob Learned

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
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.