まず第一に、私は ASP.NET MVC C# と EF に非常に慣れていません。私は現在、これら 3 つのすばらしい技術を学ぶのに役立つ Web サイトを作成中です。そうは言っても、私のプロジェクトには次のモデルがあります。
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public string FirstName { get; set; }
public string Lastname { get; set; }
public string EmailAddress { get; set; }
//public string PhoneNumber { get; set; }
public bool? ChangePassword { get; set; }
public bool? Deletable { get; set; }
//Add more Properties for more fields
public virtual ICollection<CompanyInformation> ParentCompanies { get; set; }
public virtual StaffProfile sProfile { get; set; }
}
と
public class StaffProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int StaffProfileId { get; set; }
public string Alias { get; set; }
public StaffGrouping Group { get; set; }
public ICollection<PhoneNumber> PhoneNumbers { get; set; }
public bool isPhoneNumberDisplayed { get; set; }
public bool TextNotificationsAllowed { get; set; }
public bool EmailNotificationsAllowed { get; set; }
public bool PhoneNotificationsAllowed { get; set; }
}
スタッフのグループ化
public class StaffGrouping
{
public int id { get; set; }
public string GroupName { get; set; }
}
完全を期すために、電話番号モデル
public class PhoneNumber
{
public int id { get; set; }
public string Number { get; set; }
public string Extension { get; set; }
public PhoneType Type { get; set; }
public bool isPrimary { get; set; }
public bool isInActive { get; set; }
}
public enum PhoneType {
Home,
Mobile,
Work,
Other
}
データベースからすべてのスタッフ (電話番号、ユーザープロファイル、リンク先のグループを含む) を取得し、それをビューモデルに追加して、ビューとの統合を改善しようとしています。現在、私はそのようにやっています:
public ActionResult ManageStaff() { using (var repo = new CompanyInformationRepository(new UniteOfWorkCompanies())) { var company = repo.FindCompany(User.Identity.Name);
var Users = repo.CompanyStafflookup(company);
var model = new List<StaffManagementViewModel>();
foreach (var user in Users)
{
var group = repo.StaffGroupLookup(user.sProfile);
//var phoneNumber = user.sProfile.PhoneNumbers.Where(p => p.isPrimary == true).FirstOrDefault();
model.Add(new StaffManagementViewModel
{
FirstName = user.FirstName,
LastName = user.Lastname,
EmailAddress = user.EmailAddress,
PhoneNumber = "(915) 433 - 1739", //phoneNumber.Number,
Group = group.GroupName,
UserID = user.UserId
});
}
return View(model);
}
そして私のリポジトリ:
public IQueryable<HoursOfOperation> CompanyHoursLookup(string userName)
{
var company = FindCompany(userName).id;
//var model = _db.HoursOfOperations.Where(e => e.Company.id == company);
var model = from h in _db.HoursOfOperations
where h.Company.id == company
orderby h.Day ascending, h.From ascending
select h;
return model;
}
public IQueryable<UserProfile> CompanyStafflookup(CompanyInformation company)
{
var model = from s in _db.UserProfiles.Include("sProfile")
where s.ParentCompanies.Any(e => e.companyName == company.companyName)
orderby s.FirstName ascending
select s;
return model;
}
public StaffGrouping StaffGroupLookup(StaffProfile Staff)
{
var Staffwithgroup = _db.StaffProfiles.Where(e => e.StaffProfileId == Staff.StaffProfileId).Include("Group").FirstOrDefault();
return Staffwithgroup.Group;
}
データベースへの少なくとも 3 回のトリップを数えているので、これを行うためのより効率的な方法があるはずだと思います。を使用しようとし.include
ましたuserprofile
が、グループを指すナビゲーション プロパティがないため、エラーが発生します。私が話しているコードは次のとおりです。
var model = from s in _db.UserProfiles.Include("sProfile").Include("PhoneNumbers").Include("Group")
where s.ParentCompanies.Any(e => e.companyName == company.companyName)
orderby s.FirstName ascending
select s;
UserProfiles
基本的にを含む のリストを返し、最後にStaffProfile
を含む1 つの呼び出しでこれを達成する方法はありますか?PhoneNumbers
Group