0

首先,我对 ASP.NET MVC C# 和 EF 非常陌生。我正在创建一个网站,希望能帮助我学习这三种奇妙的技术。话虽如此,我的项目中有以下模型。

[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
    }

我正在尝试从数据库中获取所有员工(包括他们链接到的电话号码、用户资料和组)并将其添加到视图模型中,以便更好地与我的视图集成。目前我正在这样做:

公共 ActionResult ManageStaff() { 使用 (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;
    }

我猜应该有更好更有效的方法来做到这一点,因为我正在计算至少三次访问数据库。我尝试在 the.include上使用userprofilebut 因为我没有指向该组的导航属性,所以它给了我一个错误。我正在谈论的代码如下:

        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包括PhoneNumbers,最后是Group

4

1 回答 1

1

您可以简单地为包含添加完整路径的前缀,即使用:

Include("sProfile.Group") 

这将包括 StaffProfile 和它的组。

于 2013-02-03T00:03:25.460 回答