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.
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:
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:
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.
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
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.