简体   繁体   中英

How to store hierarchy in Entity Framework

Take a look at the following 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. 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...

My questions:

  1. How can I achieve this UI with one code base supporting mobile, tablet and desktop interfaces?
  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.

  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. You can find more about it here: 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.

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. 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. Category2 has a parent of type Category1 and both categories contain a collection of items. Pluralsight has an excellent course on EF code first which covers all the basics.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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