Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用EF设计具有层次关系表的模型?_C#_Asp.net Mvc_Entity Framework - Fatal编程技术网

C# 如何使用EF设计具有层次关系表的模型?

C# 如何使用EF设计具有层次关系表的模型?,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,如果我有4个具有层次结构的表,例如: 建筑物(建筑物有多个楼层。一层、二层、三层…)-一层 楼层(楼层也有许多房间。101、201、301..)-第2层 房间(房间也有很多便利设施。电脑,房间冷却器…)-第3层 舒适度-四级 以下是我的模型概念: 建筑模型 public class Building { [Key] public string BuildingId { get; set; } public string BuildingName { get; set; }

如果我有4个具有层次结构的表,例如:

  • 建筑物(建筑物有多个楼层。一层、二层、三层…)-一层
  • 楼层(楼层也有许多房间。101、201、301..)-第2层
  • 房间(房间也有很多便利设施。电脑,房间冷却器…)-第3层
  • 舒适度-四级
  • 以下是我的模型概念:

    建筑模型

    public class Building
    {
        [Key]
        public string BuildingId { get; set; }
        public string BuildingName { get; set; }
        public virtual ICollection<Floor> floors { get; set; }    
    }
    
    对吗?还有什么我需要的吗


    我试了一整天,但它太简单了,无法理解和表达我的概念,因为我不擅长C#语法或.NET。

    看起来不错,你还想做什么。在一天结束时,您将使用一些数据库,从数据库的角度来看,它似乎是完美的结构。去吧

    如果我是你,我会使用NoSQL数据库,比如MongoDB

    它不使用,而是使用彼此相似的集合

    使用集合的好处是它可以包含数组或

    JSON对象中的另一个JSON对象

    它甚至可以包含JavaScript函数和数组

    此外,许多不同的房间可以有不同类型的键和值,因为

    MongoDB是无模式的

    示例:收藏“建筑”

    通过这种设计,您可以使用MongoDB实现所需的功能

    public class Floor
    {
        [Key]
        public string BuildingId { get; set; }
        [Key]        
        public string FloorId { get; set; }
        public virtual Building Building { get; set; }
        public virtual ICollection<Room> rooms { get; set; }
    }
    
    public class Room
    {
        [Key]        
        public string BuildingId { get; set; }
        [Key]
        public string FloorId { get; set; }
        [Key]
        public string RoomId { get; set; }
        public virtual Building Building { get; set; }
        public virtual Floor Floor { get; set; }
        public virtual ICollection<Amenity> amenities { get; set; }
    }
    
    public class Amenity 
    {
        [Key]
        public string AmenityId { get; set; }
        [ForeignKey("Builiding")]
        public string BuildingID{ get; set; }
        [ForeignKey("Floor")]
        public string FloorID{ get; set; }
        [ForeignKey("Room")]
        public string RoomID{ get; set; }
    }
    
    {
      name: 'buildingName',
      floor: [
        '1': [
          'room1': {
            'amenity': [ 'door', 'toilet', 'bed', 'window' ]
          },
          // ...
        ],
        // ...
      ], // ...
    }