我有以下实体框架代码片段,其中包含“Groups”表和子“ApplicationsGroupsLK”表,其中包含我需要的ApplicationID字段。
IEnumerable<Groups> Groups = DbContext.Groups.Include("ApplicationsGroupsLK").Where(p => p.GroupNumber > 0);
子数据显然在一个集合中回来了。
我基本上需要显示父数据和子ApplicationID字段(许多应用程序为1组)。
第二篇文章:
此外,从实体框架生成以下模型:
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 { 使用系统; 使用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();
}
}
Seventh Post - 这是我之前发布的错误的模型。特雷,如果你能提供帮助,我真的会感激他......我“死在水中”,除非我能让这部分工作。
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;
}
答案 0 :(得分:1)
这需要稍微调整思考。 Grid只接受平面模型。经常会问这类问题,这是一个初答:Kendo UI Grid - How to Bind to Child Properties
如果这没有帮助,请在此处发布更多代码,我可以与您一起完成。