更新:问题解决:
所以我设法解决了这个问题。我之前无法获得任何数据的原因是EF试图访问一个名为"NodeView“的表,但是没有这样的表,而是视图,因此没有结果。所以我才这么做
var test = _context.NodeView.FromSql("SELECT * FROM dbo.nodeIcons");
var nodes = from n in test
where n.Rootid == 1
orderby n.Parentid, n.Lvl, n.Position ascending
select new TreeNode()我现在直接访问视图,然后根据结果创建TreeNode对象
我在使用实体框架6从SQL视图访问数据时遇到了这个问题,但无法找到解决方案。
我在做什么?:
我目前正在开发的树视图使用.NET核心与EF 6和角5。
这就是我的后端当前的样子
视图模型
public class NodeView
{
public int Id { get; set; }
public string Name { get; set; }
public string Descr { get; set; }
public int Rootid { get; set; }
public int Parentid { get; set; }
public int? Lvl { get; set; }
public int? Position { get; set; }
public string Expanded { get; set; }
public string Collapsed { get; set; }
public string DefIcon { get; set; }
}节点模型
public partial class Node
{
public int Id { get; set; }
public string Name { get; set; }
public string Descr { get; set; }
public int Rootid { get; set; }
public int Parentid { get; set; }
public int? Lvl { get; set; }
public int? Position { get; set; }
}DBContext
public partial class NodeContext : DbContext
{
public virtual DbSet<Node> Node { get; set; }
public virtual DbSet<NodeView> NodeView { get; set; }
public NodeContext(DbContextOptions<NodeContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Node>(entity =>
{
entity.ToTable("node");
entity.Property(e => e.Id)
.HasColumnName("ID")
.ValueGeneratedNever();
entity.Property(e => e.Descr)
.IsRequired()
.HasColumnName("DESCR")
.HasMaxLength(256)
.IsUnicode(false);
entity.Property(e => e.Lvl).HasColumnName("LVL");
entity.Property(e => e.Name)
.IsRequired()
.HasColumnName("NAME")
.HasMaxLength(256)
.IsUnicode(false);
entity.Property(e => e.Parentid).HasColumnName("PARENTID");
entity.Property(e => e.Position).HasColumnName("POSITION");
entity.Property(e => e.Rootid).HasColumnName("ROOTID");
});
modelBuilder.Entity<NodeView>(entity => { entity.HasKey(e => e.Id); });
}
}在我的控制器中,我正在创建这个DTO类的treenode对象。
public class TreeNode
{
public string Label { get; set; }
public string Data { get; set; }
public string ExpandedIcon { get; set; }
public string CollapsedIcon { get; set; }
public List<TreeNode> Children { get; set; }
public int ParentId { get; set; }
public int RootId { get; set; }
public int Level { get; set; }
public int Position { get; set; }
public int ID { get; set; }
public string Icon { get; set; }
public TreeNode()
{
}
public void AddChild(TreeNode child)
{
if(Children == null)
{
Children = new List<TreeNode>();
}
Children.Add(child);
}
}我的前端需要一个特定的数据结构。目前,我正在我的控制器中创建这个
[HttpGet]
public List<TreeNode> GetNodes()
{
var nodes = from n in _context.NodeView
where n.Rootid == 1
orderby n.Parentid, n.Lvl, n.Position ascending
select new TreeNode()
{
ID = n.Id,
Label = n.Name,
Data = n.Descr,
ExpandedIcon = n.Expanded,
CollapsedIcon = n.Collapsed,
Icon = n.DefIcon,
RootId = n.Rootid,
ParentId = n.Parentid,
Level = n.Lvl.Value,
Position = n.Position.Value
};
Dictionary<int,TreeNode> parentDict = new Dictionary<int, TreeNode>();
List<TreeNode> rootTree = new List<TreeNode>();
foreach (TreeNode aNode in nodes)
{
if(aNode.ParentId == 0)
{
parentDict.Add(aNode.ID, aNode);
rootTree.Add(aNode);
}
else
{
TreeNode fatherNode = parentDict[aNode.ParentId];
fatherNode.AddChild(aNode);
parentDict.Add(aNode.ID, aNode);
}
}
return rootTree;
}我创建了一个列表节点,并从视图NodeView中添加了所有数据
之后,我将遍历该列表,并查找每个节点的子/父节点。
ParentID == 0是根节点。树的第一个节点。其他一切都是子节点,也可能是父节点。在else子句中找到一个孩子之后,我正在寻找它的父亲,并将其添加到parentDict中。最后,我将返回包含整个树的rootTree。
使用我的节点模型可以正常工作。但是,它不适用于*NodeView模型**
错误消息:无效的对象名称: NodeView
我真的不知道是什么导致了这个问题,但我相信,要么是在DBContext中的某个地方,我在底部为NodeView创建了第二个modelbuilder函数,要么是在选择LINQ语句中返回零对象的视图时做错了什么,因此我不能在foreach循环中迭代。
=>程序在foreach循环的开始处停止
发布于 2018-03-08 14:42:05
所以我设法解决了这个问题。我之前无法获得任何数据的原因是EF试图访问一个名为"NodeView“的表,但是没有这样的表,而是视图,因此没有结果。所以我才这么做
var test = _context.NodeView.FromSql("SELECT * FROM dbo.nodeIcons");
var nodes = from n in test
where n.Rootid == 1
orderby n.Parentid, n.Lvl, n.Position ascending
select new TreeNode()我现在直接访问视图,然后根据结果创建TreeNode对象
https://stackoverflow.com/questions/49174564
复制相似问题