从db获取链接模型值的更好方法是什么?

时间:2013-02-02 21:43:31

标签: c# asp.net-mvc linq asp.net-mvc-4 ef-code-first

首先,我是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
    }

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

public 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;
    }

我猜测应该有一个更好的更有效的方法,因为我至少计算三次访问数据库。我尝试使用.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,其中包含PhoneNumbers,最后是Group

1 个答案:

答案 0 :(得分:1)

您可以简单地在include前面加上完整路径,即使用:

Include("sProfile.Group") 

这将包括StaffProfile和它的Group。