【问题标题】:What would be the better way of getting linked models values from db从 db 获取链接模型值的更好方法是什么
【发布时间】:2013-02-02 21:43:30
【问题描述】:

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

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

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

【问题讨论】:

  • @MortenMertner 非常感谢,我认为必须有办法做到这一点,但只是不知道你可以把完整的路径放在这里。就像我说的,我对整个 asp.net mvc 非常陌生,并且正在一步一步地学习。您介意将此作为答案提交,以便我将其标记为已解决吗?

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


【解决方案1】:

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

Include("sProfile.Group") 

这将包括 StaffProfile 和它的组。

【讨论】:

    猜你喜欢
    • 2018-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-02
    • 2019-01-29
    • 2014-05-03
    • 2010-09-28
    • 1970-01-01
    相关资源
    最近更新 更多