C# 从数据库中获取链接模型值的更好方法是什么

C# 从数据库中获取链接模型值的更好方法是什么,c#,asp.net-mvc,linq,asp.net-mvc-4,ef-code-first,C#,Asp.net Mvc,Linq,Asp.net Mvc 4,Ef Code First,首先,我对ASP.NETMVC和EF非常陌生。我正在创建一个网站,希望能帮助我学习这三项奇妙的技术。话虽如此,我的项目中有以下模型 [Table("UserProfile")] public class UserProfile { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int UserId { get; set; } public string Use

首先,我对ASP.NETMVC和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 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
    }
我正在尝试从数据库中获取所有员工(包括他们链接到的电话号码、用户配置文件和组),并将其添加到viewmodel中,以便更好地与我的视图集成。目前我正在这样做:

公共行动结果管理人员() { 使用(var repo=new CompanyInformationRepository(new UniteOfWorkCompanies())) { var company=repo.FindCompany(User.Identity.Name)


有没有一种方法可以在一次调用中实现这一点,它基本上会返回一个
用户配置文件列表
,其中包括
员工配置文件
,其中包括
电话号码
,最后是

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

Include("sProfile.Group") 

这将包括StaffProfile及其组。

Afaik您只需在include前面加上完整路径,即使用
include(“sProfile.Group”)
包括StaffProfile和它的组。@MortenMertner非常感谢,我想一定有办法做到这一点,但只是不知道你可以在这里设置完整的路径。正如我所说,我对整个asp.net mvc非常陌生,正在一步一步地学习。你介意将此作为答案提交,以便我将其标记为已解决吗?
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
    }
            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;
    }
        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;
Include("sProfile.Group")