简体   繁体   English

如何在实体框架中存储层次结构

[英]How to store hierarchy in Entity Framework

Take a look at the following UI: 看一下以下UI:

ui样机

I am allowing the user, who owns an auto shop of some kind, to select what types of service his shop provides. 我允许拥有某种汽车修理店的用户选择其商店提供的服务类型。 There are 5 levels to my hierarchy and thousands of total entries. 我的层次结构有5个级别,并且共有数千个条目。 Selecting (checking) any skill area automatically selects all sub-areas or child areas. 选择(选中)任何技能区域都会自动选择所有子区域或子区域。 Each area has an ID which for the top level looks like 10000, the next level 11000, etc... 每个区域都有一个ID,顶层的ID看起来像10000,下一层的ID看起来像11000,依此类推...

My questions: 我的问题:

  1. How can I achieve this UI with one code base supporting mobile, tablet and desktop interfaces? 如何通过一个支持移动,平板电脑和台式机界面的代码库来实现此UI?
  2. How can I represent the hierarchy using Entity Framework for populating the hierarchy as well as storing the user's selections? 如何使用实体框架表示层次结构以填充层次结构以及存储用户的选择?

The application is in ASP.Net MVC 4 using SimpleMembership. 该应用程序在使用SimpleMembership的ASP.Net MVC 4中。

  1. The best way to have one code base supporting multiple platforms is to use ASP.NET MVC 4 WebAPI (JSON or XML) as a REST web service. 具有一个代码库支持多个平台的最佳方法是使用ASP.NET MVC 4 WebAPI(JSON或XML)作为REST Web服务。 You can find more about it here: http://www.asp.net/web-api 您可以在这里找到有关它的更多信息: http : //www.asp.net/web-api

There are also a couple of ready-to-use parsers to deserialize the JSON/XML data into C# object on the client side. 在客户端,还有两个易于使用的解析器将JSON / XML数据反序列化为C#对象。

Second question seems to be more related to the database schema behind the Entity Framework abstraction. 第二个问题似乎与实体框架抽象背后的数据库架构更相关。 In terms of EF probably the best way to organize your data is to have categories referencing other categories with the last category referencing the actual services. 就EF而言,组织数据的最佳方法可能是让类别引用其他类别,而最后一个类别引用实际的服务。 Are you familiar with the concept of database relations, foreign keys, etc? 您是否熟悉数据库关系,外键等概念?

Edit: 编辑:

    public class Category1{
    public string Name { get; set; }

    public virtual ICollection<Item> Items { get; set; }
}

public class Category2{
    public string Name { get; set; }

    public Category1 ParentCategory { get; set; }

    public virtual ICollection<Item> Items { get; set; }
}

public class Item{
    public string Name { get; set; }
    public decimal Price { get; set; }
}

This is an example how would you write code-first EntityFramework objects. 这是一个示例,您将如何编写代码优先的EntityFramework对象。 Category2 has a parent of type Category1 and both categories contain a collection of items. Category2的父类型为Category1,并且两个类别都包含项的集合。 Pluralsight has an excellent course on EF code first which covers all the basics. Pluralsight首先开设了有关EF代码的优秀课程 ,涵盖了所有基础知识。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM