Eager loading with include method does not return data from collection - asp.net-mvc

I wanted to eager load a collection but it does not returns any data
I have tried nothing
//here is my controller
var accountGroup = await db.AccountGroups.Include(a=>a.AccountGroupTypes)
.Include(a=>a.AccountPrimaryGroups).ToListAsync();
//here is my model
public int? Id { get; set; }
public string Name { get; set; }
public int AccountGroupTypeId { get; set; }
public ICollection<AccountGroupType> AccountGroupTypes { get; set; }
public int AccountPrimaryGroupId { get; set; }
public ICollection<AccountPrimaryGroup> AccountPrimaryGroups { get; set; }
public DateTime CreationDateUtcNow{ get; set; }
It returns AccountGroupData but It does not return AccountGroupTypes and AccountPrimaryGroup data

Mark your AccountGroupTypes and AccountPrimaryGroups as virtual properties and be sure that you have a parameterless constructor which is initializing those properties.
public int? Id { get; set; }
public string Name { get; set; }
public int AccountGroupTypeId { get; set; }
public vitual ICollection<AccountGroupType> AccountGroupTypes { get; set; }
public int AccountPrimaryGroupId { get; set; }
public virtual ICollection<AccountPrimaryGroup> AccountPrimaryGroups { get; set; }
public DateTime CreationDateUtcNow{ get; set; }
public AccountGroup()
{
this.AccountGroupTypes = new HashSet<AccountGroupType>();
this.AccountPrimaryGroups = new HashSet<AccountPrimaryGroups>();
}

Related

Object reference error when getting data from database

My portal has worked fine for several years but nowadays I have problems with my some codes. I want to update my code but at the first step I have problems with login page. when I use FirstOrDefault I got error on object reference while the list is filled and contains 1 user.
int g = db.sp_GetUserData_ByUserName(model.UserName.Trim()).Count();
//it shows 1
OSUserData userData1 = db.sp_GetUserData_ByUserName(model.UserName.Trim()).FirstOrDefault();
I didn't change this code and It works perfectly on the portal. However, I can't run it on my development server.
I checked again and I found that OSUserData constructor throws exception : Enumeration yielded no result. this is my class code :
public OSUserData()
{
this.OSUploadedFilesMultipleUsers = new HashSet<OSUploadedFilesMultipleUser>();
this.OSSalaryLists = new HashSet<OSSalaryList>();
this.OSUploadedFiles = new HashSet<OSUploadedFile>();
}
public int Id { get; set; }
public string Address { get; set; }
public string BranchCode { get; set; }
public string BranchParentNationalCode { get; set; }
public string CitName { get; set; }
public string City { get; set; }
public string CompanyName { get; set; }
public string CouName { get; set; }
public string Country { get; set; }
public string FaxNo { get; set; }
public string FollowCode { get; set; }
public string Hozeh { get; set; }
public Nullable<bool> IsBranch { get; set; }
public string LoginStatus { get; set; }
public string NationalCode { get; set; }
public string NewEconomicNo { get; set; }
public string Oldeconomicno { get; set; }
public string PostalCode { get; set; }
public string ProIdTTMS { get; set; }
public string ProName { get; set; }
public string Provision { get; set; }
public string RegistrationNo { get; set; }
public string State { get; set; }
public string TaxOfficeCode { get; set; }
public string TaxPayerType { get; set; }
public string TelCode { get; set; }
public string TelNo { get; set; }
public string TprId { get; set; }
public string UserName { get; set; }
public bool Validate { get; set; }
public bool Disabled { get; set; }
public string OwnerShipTypeDesc { get; set; }
public Nullable<byte> OwnerShipTypeCode { get; set; }
public bool Processed { get; set; }
public bool CheckedUser { get; set; }
public bool Blocked { get; set; }
public virtual ICollection<OSUploadedFilesMultipleUser> OSUploadedFilesMultipleUsers { get; set; }
public virtual ICollection<OSSalaryList> OSSalaryLists { get; set; }
public virtual ICollection<OSUploadedFile> OSUploadedFiles { get; set; }
You are trying to add to a null/unreferenced/not initialized object/list. Initialize first the list
List<OSUserData> userData1 = new List<OSUserData>();
Then add it
var user = db.sp_GetUserData_ByUserName(model.UserName.Trim()).FirstOrDefault();
userData1.Add(new OSUserData{
Prop1 = user.Prop1,
Prop2 = user.Prop2,
......
});
Or if you only want one user, just make it var
var user = db.sp_GetUserData_ByUserName(model.UserName.Trim()).FirstOrDefault();

Automapper with nested entity not working

I have two classes which are mentioned below. I am trying to map those through Auto Mapper but it is not working.
public class VMTemplates
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Path { get; set; }
public bool IsActive { get; set; }
public System.DateTime TimeStamp { get; set; }
public virtual ICollection<VMTemplateGroup> VMTemplateGroup { get; set; }
}
public VMTemplateViewModel()
{
VMTemplateGroup = new List<VMTemplateGroupViewModel>();
}
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Description { get; set; }
public string Path { get; set; }
public bool IsActive { get; set; }
[Required(ErrorMessage = "Please Upload File")]
[Display(Name = "Upload File")]
[ValidateFile]
public HttpPostedFileBase TemplateUpload { get; set; }
public System.DateTime TimeStamp { get; set; }
public List<VMTemplateGroupViewModel> VMTemplateGroup { get; set; }
I am using this code to map them
confi.CreateMap<VMTemplateViewModel, VMTemplates>().ReverseMap();
confi.CreateMap<VMTemplateGroupViewModel, VMTemplateGroup>().ReverseMap();
calling code
public VMTemplates GetVMTemplateById(int id)
{
return DataContext.VMTemplates.Include("VMTemplateGroup").Where(a => a.Id == id).FirstOrDefault();
}
This is where exception happens. I get stack overflow exception
public VMTemplateViewModel GetVMTemplateById(int templateId)
{
var result = _vmTemplateRepository.GetVMTemplateById(templateId);
return _autoMapperService.MapEntity<VMTemplateViewModel>(result);
}

TableName.RowName retrieves Nothing in MVC

I have a model class called "KelimeTuru" designed as shown below:
public partial class KelimeTuru
{
public int Id { get; set; }
public int KelimeId { get; set; }
public int MenseiId { get; set; }
public int TurId { get; set; }
public int AnlamTurleriId { get; set; }
public int SozlukTuruId { get; set; }
public Nullable<int> SesId { get; set; }
public Nullable<int> VideoId { get; set; }
public Nullable<int> ResimId { get; set; }
public Nullable<int> SembolId { get; set; }
public Nullable<int> BirimId { get; set; }
public Nullable<int> SimgeId { get; set; }
public string Anlam { get; set; }
public string Transkript { get; set; }
public virtual AnlamTurleri AnlamTurleri { get; set; }
public virtual Birim Birim { get; set; }
public virtual Kelime Kelime { get; set; }
public virtual Mensei Mensei { get; set; }
public virtual Sembol Sembol { get; set; }
public virtual Ses Ses { get; set; }
public virtual Simge Simge { get; set; }
public virtual SozlukTuru SozlukTuru { get; set; }
public virtual Turler Turler { get; set; }
public virtual Video Video { get; set; }
public virtual Resim Resim { get; set; }
}
And It retrieves the required data if we use the Id of the record properly like this:
public ActionResult Details(int id=0)
{
SozlukEntities db = new SozlukEntities();
KelimeTuru kelime = db.KelimeTuru.Find(id);
if (kelime == null)
{
return HttpNotFound();
}
return View(kelime);
}
I want to retrieve records according to their "KelimeId"s not "Id"s so replaced the word "id" with "KelimeId" in my code. But got a "404.0 - Not Found" error.
Could you please help me retrieve "KelimeTuru" table using KelimeId?
try this:
KelimeTuru kelime = db.KelimeTuru.SingleOrDefault(x => x.KelimeId == id);
Find method works only on Primary Key Property, means if your property is decorated with [Key] attribute, then Find() will be able to find record, otherwise it wil not fetch the record.
It sounds like you want something like this:
KelimeTuru kelime = db.KelimeTuru.Where(x => x.KelimeId == id).SingleOrDefault();

simple lookups in Data Access Layer, Business Layer or in UI?

ASP .NET MVC4
Class #1:
public class F61BPROD
{
public int WPDOCO { get; set; }
public string WPDCTO { get; set; }
public string WPMCU { get; set; }
public string WPLOCN { get; set; }
public string WPDCT { get; set; }
public int WPTRDJ { get; set; }
public string WPKYPR { get; set; }
public string WPLITM { get; set; }
public decimal WPTRQT { get; set; }
public string WPKYFN { get; set; }
public string WPLOTN { get; set; }
public string WPLRP1 { get; set; }
public string WPLRP2 { get; set; }
public string WPLRP3 { get; set; }
public string WPLRP4 { get; set; }
public string WPLRP5 { get; set; }
public string WPLRP6 { get; set; }
public string WPLRP7 { get; set; }
public string WPLRP8 { get; set; }
public string WPLRP9 { get; set; }
public string WPLRP0 { get; set; }
public string WPFLAG { get; set; }
public string WPLOT1 { get; set; }
public string WPLOT2 { get; set; }
}
For one of the properties of Class #1 i need to fetch one of Class #2:
public class JDEItemBasic
{
public int itm { get; set; }
public string litm { get; set; }
public string dsc { get; set; }
public string dsce { get; set; }
public string ean14 { get; set; }
public string cc { get; set; }
public string uom1 { get; set; }
public string uom2 { get; set; }
public int uom1ea { get; set; }
public int bxuom1 { get; set; }
public int uom1gr { get; set; }
}
There is a DAL that gets the above classes. I need to combine these classes a new class that will have most of the properties of the above classes.
Should i create a third class and do the job in BLL?
or should i do it in UI using LINQ to Entities after i fetch them?
Should i create a third class and do the job in BLL?
or should i do it in UI using LINQ to Entities after i fetch them?
That would depend on where you need this class. If it is for displaying purposes then it should live in the UI. This class even has a name in this case: it's called a view model and is what your controller action could pass to the view after querying your DAL layer and projecting the various results to this view model.

Display value in label using values in different table in razor view

I am looking to display Hole_Num value from my Holes class, in my RoundDetails class within the RoundDetails/Create razor view.
Here is my Holes class:
public partial class Hole
{
public Hole()
{
this.RoundDetails = new HashSet();
}
public int HoleId { get; set; }
public int FacilityId { get; set; }
public int CourseId { get; set; }
public int TeeTypeId { get; set; }
public int Hole_Num { get; set; }
public int Yardage { get; set; }
public byte Par { get; set; }
public short Handicap { get; set; }
public virtual ICollection<RoundDetail> RoundDetails { get; set; }
public virtual TeeType TeeType { get; set; }
}
Here is my RoundDetails class:
public partial class RoundDetail
{
public int RoundId { get; set; }
public int GolferId { get; set; }
public int FacilityId { get; set; }
public int CourseId { get; set; }
public int TeeTypeId { get; set; }
public int HoleId { get; set; }
public decimal Differential { get; set; }
public Nullable Date { get; set; }
public byte Score { get; set; }
public Nullable Putts { get; set; }
public Nullable GIR { get; set; }
public Nullable FIR { get; set; }
public Nullable Up_And_Down { get; set; }
public Nullable Par_Save { get; set; }
public Nullable Sand_Save { get; set; }
public Nullable Driving_Distince { get; set; }
public Nullable Proximity { get; set; }
public Nullable Green_Fee { get; set; }
public virtual Course Course { get; set; }
public virtual Facility Facility { get; set; }
public virtual Golfer Golfer { get; set; }
public virtual Hole Hole { get; set; }
public virtual TeeType TeeType { get; set; }
}
Here is my razor view:
<td>
#Html.LabelFor(model => model.Hole.Par)
</td>
How am I able to populate this label with the Par information from the Holes table? I assume that some manipulation with the Create method of the RoundDetailController or creating a ViewModel of some sort is the way to go but I"m not sure which one or how start either. Please help!

Resources