0

我有以下实体框架代码片段,它有一个“Groups”表和一个包含我需要的 ApplicationID 字段的子“ApplicationsGroupsLK”表。

IEnumerable<Groups> Groups = DbContext.Groups.Include("ApplicationsGroupsLK").Where(p => p.GroupNumber > 0);

子数据显然在集合中返回。

我基本上需要显示父数据以及子 ApplicationID 字段(许多应用程序到 1 个组)。

在我的 MVC 视图中,我的 ViewModel 应该是什么样的,它将包含我需要的返回的父数据和子数据,以便我可以正确地将其绑定到网格?

第二个帖子:

此外,以下模型是从实体框架生成的:

public partial class Project
    {
        public Project()
        {
            this.TimeTrackings = new HashSet<TimeTracking>();
        }

     [DataMember]
        public short ProjectID { get; set; }
     [DataMember]
        public short CustomerID { get; set; }
     [DataMember]
        public string Name { get; set; }
     [DataMember]
        public string Description { get; set; }
     [DataMember]
        public short CategoryID { get; set; }
     [DataMember]
        public short PriorityID { get; set; }
     [DataMember]
        public short StatusID { get; set; }
     [DataMember]
        public Nullable<decimal> Quote { get; set; }
     [DataMember]
        public string Notes { get; set; }
     [DataMember]
        public System.DateTime CreatedDate { get; set; }
     [DataMember]
        public Nullable<System.DateTime> UpdatedDate { get; set; }

     [DataMember]
        public virtual Category Category { get; set; }
     [DataMember]
        public virtual Customer Customer { get; set; }
     [DataMember]
        public virtual Priority Priority { get; set; }
     [DataMember]
        public virtual Status Status { get; set; }
     [DataMember]
        public virtual ICollection<TimeTracking> TimeTrackings { get; set; }
    }

可以看到 TimeTrackings 是 Project 的子表。您还可以看到 CategoryID、CustomerID、PriorityID 和 StatusID 是父表具有的外键。在这种情况下,我只对 CategoryID FK 感兴趣。

我还没有这样做(不是在我家里的机器上),但是当我将数据输入这个模型时,公共虚拟类别类别字段中实际包含什么?由于不是集合,查询执行后该字段返回什么数据。

第三篇文章: Telerik asp.net 用于 DB 调用的 mvc 语法:

IEnumerable<Groups> GroupList = db.GetGroups();

            return View(new GridModel<Groups>
            {
                Data = GroupList
            });

第四篇文章:Trey,下面是我为我的目的修改的代码,希望你能在我实现它之前检查它。我想我理解它并且看起来很棒......

public partial class Project
    {
        public Project()
        {
            this.TimeTrackings = new HashSet<TimeTracking>();
        }

     [DataMember]
        public short ProjectID { get; set; }
     [DataMember]
        public short CustomerID { get; set; }
     [DataMember]
        public string Name { get; set; }
     [DataMember]
        public string Description { get; set; }
     [DataMember]
        public short CategoryID { get; set; }
     [DataMember]
        public short PriorityID { get; set; }
     [DataMember]
        public short StatusID { get; set; }
     [DataMember]
        public Nullable<decimal> Quote { get; set; }
     [DataMember]
        public string Notes { get; set; }
     [DataMember]
        public System.DateTime CreatedDate { get; set; }
     [DataMember]
        public Nullable<System.DateTime> UpdatedDate { get; set; }
     [DataMember]
        public short ApplicationID { get; set; }
     [DataMember]
        public string ApplicationName { get; set; }
     [DataMember]
        public virtual Category Category { get; set; }
     [DataMember]
        public virtual Customer Customer { get; set; }
     [DataMember]
        public virtual Priority Priority { get; set; }
     [DataMember]
        public virtual Status Status { get; set; }
     [DataMember]
        public virtual ICollection<TimeTracking> TimeTrackings { get; set; }


     public ProjectModel(Project project)
     {
        ProjectID = project.ProjectID;
        CustomerID = project.CustomerID;
        Name = project.Name;
        Description = project.Description;
    CategoryID = project.CategoryID;
        PriorityID = project.PriorityID;
        StatusID = project.StatusID;
        Quote = project.Quote;
        Notes = project.Notes;
        CreatedDate = project.CreatedDate;
        UpdatedDate = project.UpdatedDate;
        ApplicationID = project.ApplicationsGroupsLK.ApplicationID;
        ApplicationName = project.ApplicationsGroupsLK.ApplicationName;
     }


     // Neat Linq trick to convert database query results directly to Model
     public static IList<ProjectModel> FlattenToThis(IList<Project> projects)
     {
        return projects.Select(project => new ProjectModel(project)).ToList();
     }

    }

第五帖:

using (wmswebEntities DbContext = new wmswebEntities())
                {
                    DbContext.Configuration.ProxyCreationEnabled = false;
                    DbContext.Database.Connection.Open();

                    IEnumerable<Projects> projects = DbContext.Projects.Where(p => p.GroupNumber > 0);

                    IList<ProjectModel> results = Project.FlattenToThis(projects);

                    return results

}

第六篇命名空间CMSEFModel { using System; 使用 System.Collections.Generic;

public partial class GroupModel
{
    public GroupModel()
    {
        this.ApplicationsGroupsLKs = new HashSet<ApplicationsGroupsLK>();
        this.GroupApplicationConfigurationsLKs = new HashSet<GroupApplicationConfigurationsLK>();
        this.UsersGroupsLKs = new HashSet<UsersGroupsLK>();
    }

    public int GroupNumber { get; set; }
    public string GroupName { get; set; }
    public int GroupRank { get; set; }
    public bool ActiveFlag { get; set; }
    public System.DateTime DateAdded { get; set; }
    public string AddedBy { get; set; }
    public System.DateTime LastUpdated { get; set; }
    public string LastUpdatedBy { get; set; }
    // Application - Lazy Loading population
    public int ApplicationID { get; set; }
    // UsersGroupsLK - Lazy Loading population
    public int UserNumber { get; set; }
    public string UserID { get; set; }

    public virtual ICollection<ApplicationsGroupsLK> ApplicationsGroupsLKs { get; set; }
    public virtual ICollection<GroupApplicationConfigurationsLK> GroupApplicationConfigurationsLKs { get; set; }
    public virtual ICollection<UsersGroupsLK> UsersGroupsLKs { get; set; }

    public GroupModel()
    {}
    public GroupModel(GroupModel group)
     {
        GroupNumber = group.GroupNumber;
        GroupName = group.GroupName;
        ActiveFlag = group.ActiveFlag;
        DateAdded = group.DateAdded;
        AddedBy = group.AddedBy;
        LastUpdated = group.LastUpdated;
        LastUpdatedBy = group.LastUpdatedBy;
        UserNumber = group.UsersGroupsLKs.
     }


 // Neat Linq trick to convert database query results directly to Model
 public static IList<GroupModel> FlattenToThis(IList<GroupModel> groups)
 {
    return groups.Select(group => new GroupModel(group)).ToList();
 }
}

}

第七篇文章 - 这是我在之前发布的错误中遇到问题的模型。Trey,如果你能帮忙,我真的很感激......我“死在水里”,除非我能让这部分工作。

namespace CMSEFModel
{
    using System;
    using System.Collections.Generic;

    public partial class Group
    {
        public Group()
        {
            this.ApplicationsGroupsLKs = new HashSet<ApplicationsGroupsLK>();
            this.GroupApplicationConfigurationsLKs = new HashSet<GroupApplicationConfigurationsLK>();
            this.UsersGroupsLKs = new HashSet<UsersGroupsLK>();
        }

        public int GroupNumber { get; set; }
        public string GroupName { get; set; }
        public int GroupRank { get; set; }
        public bool ActiveFlag { get; set; }
        public System.DateTime DateAdded { get; set; }
        public string AddedBy { get; set; }
        public System.DateTime LastUpdated { get; set; }
        public string LastUpdatedBy { get; set; }
        // Application - Lazy Loading population
        public int ApplicationID { get; set; }
        // UsersGroupsLK - Lazy Loading population
        public int UserNumber { get; set; }
        public string UserID { get; set; }

        public virtual ICollection<ApplicationsGroupsLK> ApplicationsGroupsLKs { get; set; }
        public virtual ICollection<GroupApplicationConfigurationsLK> GroupApplicationConfigurationsLKs { get; set; }
        public virtual ICollection<UsersGroupsLK> UsersGroupsLKs { get; set; }

        public GroupModel(Group group)
         {
            GroupNumber = group.GroupNumber;
            GroupName = group.GroupName;
            ActiveFlag = group.ActiveFlag;
            DateAdded = group.DateAdded;
            AddedBy = group.AddedBy;
            LastUpdated = group.LastUpdated;
            LastUpdatedBy = group.LastUpdatedBy;
            UserNumber = group.UsersGroupsLKs.
         }

     // Neat Linq trick to convert database query results directly to Model
     public static IList<GroupModel> FlattenToThis(IList<Group> groups)
     {
        return groups.Select(group => new GroupModel(group)).ToList();
     }
    }

}

八贴:

using (wmswebEntities DbContext = new wmswebEntities())
                {
                    DbContext.Configuration.ProxyCreationEnabled = false;
            DbContext.Configuration.LazyLoadingEnabled = true;
                    DbContext.Database.Connection.Open();

                    List<Groups> myGroups = new List<Groups>();

                     var myGroups = from p in DbContext.Groups
                                where p.ActiveFlag = true
                                select new
                                {
                                    p.Groups.ApplicationName,
                                    p.Groups.GroupName,
                                    p.Groups.GroupRank,
                                    p.Groups.ActiveFlag,
                                    p.Groups.DateAdded,
                                    p.Groups.AddedBy,
                                    p.Groups.LastUpdated,
                                    p.Groups.LastUpdatedBy,
                                    p.Groups.ApplicationsGroupsLK.ApplicationID,
                                    p.Groups.UsersGroupsLK.UserNumber
                                };

                    return myGroups;
                }
4

1 回答 1

1

这需要稍微调整一下思路。Grid 只接受平面模型。这类问题经常被问到,这里是一个入门答案:Kendo UI Grid - How to Bind to Child Properties

如果这没有帮助,请在此处发布更多代码,我可以与您一起完成它。

于 2013-02-14T01:58:16.257 回答