实体框架 - 设置多个表关系

时间:2018-01-18 10:29:27

标签: asp.net entity-framework rest

我无法弄清楚如何在3个表之间设置正确的关系。 我正在以代码优先的方式工作。 这是我的表格:

  public class Parkings
        {
            [Key]
            public int ParkId { get; set; }
            [Required]
            public string StartDate { get; set; }
            [Required]
            public string EndDate { get; set; }

            public int CityId { get; set; }

            public int StreetId { get; set; }

            [ForeignKey("CityId")]
            public virtual Cities Cities { get; set; }

        }


        public class Cities
        {
            [Key]
            public int CityId { get; set; }
            [Required]
            public string CityDesc { get; set; }

            public virtual ICollection<Streets> Streets { get; set; }

            public virtual ICollection<Parkings> Parkings { get; set; }
        }

        public class Streets
        {
            [Key]
            public int StreetId { get; set; }

            public int CityId { get; set; }
            [Required]
            public string StreetDesc { get; set; }

            [ForeignKey("CityId")]
            public virtual Cities Cities { get; set; }



        }

首先,我想在我的代码中播种数据库,因此它不会为空。 我这样做:

public class DatabaseInitializer : DropCreateDatabaseIfModelChanges<DatabaseContext>
    {
        protected override void Seed(DatabaseContext context)
        {
            base.Seed(context);

            //
            context.Cities.Add(
        new Cities() { CityId = 1, CityDesc = "Tel Aviv" }
        );
            context.Cities.Add(
    new Cities() { CityId = 2, CityDesc = "Ramat Gan" }

    );

            context.Streets.Add(
                new Streets() { StreetId = 1, CityId = 1, StreetDesc = "Pinkas"}
                );

            context.Streets.Add(
              new Streets() { StreetId = 2, CityId = 2, StreetDesc = "Bialik" }
              );

            context.Streets.Add(
              new Streets() { StreetId = 3, CityId = 1, StreetDesc = "Dizengoff" }
              );

            context.Parkings.Add(
              new Parkings() { StreetId = 1, CityId = 1, StartDate = "2018-01-17", EndDate = "2018-01-17" }
              );

            context.Parkings.Add(
          new Parkings() { StreetId = 3, CityId = 1, StartDate = "2019-01-17", EndDate = "2019-01-17" }
          );

            context.Parkings.Add(
         new Parkings() { StreetId = 2, CityId = 2, StartDate = "2018-01-17", EndDate = "2018-01-17" }
         );

            context.SaveChanges();
        }
    }

当我正在执行查看数据库的get请求时,这是我的结果:

 {
    "cityId": 1,
    "cityDesc": "Tel Aviv",
    "streets": [
      {
        "streetId": 1,
        "streetDesc": "Pinkas",
        "parkings": [
          {
            "parkId": 1,
            "startDate": "2018-01-17",
            "endDate": "2018-01-17"
          },
          {
            "parkId": 2,
            "startDate": "2019-01-17",
            "endDate": "2019-01-17"
          }
        ]
      },
      {
        "streetId": 3,
        "streetDesc": "Dizengoff",
        "parkings": [
          {
            "parkId": 1,
            "startDate": "2018-01-17",
            "endDate": "2018-01-17"
          },
          {
            "parkId": 2,
            "startDate": "2019-01-17",
            "endDate": "2019-01-17"
          }
        ]
      }
    ]
  },
  {
    "cityId": 2,
    "cityDesc": "Ramat Gan",
    "streets": [
      {
        "streetId": 2,
        "streetDesc": "Bialik",
        "parkings": [
          {
            "parkId": 3,
            "startDate": "2018-01-17",
            "endDate": "2018-01-17"
          }
        ]
      }
    ]
  }
]

正如您所看到的,CityId工作正常,它将停车位分配给正确的城市,但它忽略了StreetId,因此停车在目标城市中重复出现。

我该如何配置它,因此它适用于CityId和amp; StreetId? 我正在做的任何测试都没有给出正确的结果..

非常感谢!

1 个答案:

答案 0 :(得分:0)

我对你的课程进行了一些修改。首先,我将它们重命名为单数形式。然后,当你在街道上找到城市的时候,我从城市中删除了不必要的停车位,最后我添加了一个缺少关系的街道停车场:

  public class Parking
        {
            [Key]
            public int ParkId { get; set; }
            [Required]
            public string StartDate { get; set; }
            [Required]
            public string EndDate { get; set; 

            public int StreetId { get; set; }

            [ForeignKey("StreetId")]
            public virtual Street Street { get; set; }

        }


        public class City
        {
            [Key]
            public int CityId { get; set; }
            [Required]
            public string CityDesc { get; set; }

            public virtual ICollection<Street> Street { get; set; }

         }

        public class Street
        {
            [Key]
            public int StreetId { get; set; }

            public int CityId { get; set; }
            [Required]
            public string StreetDesc { get; set; }

            [ForeignKey("CityId")]
            public virtual City City { get; set; }

            public virtual ICollection<Parking> Parking { get; set; }

        }

我用手机写了这个,所以我可能错过了一些阅读或写作,但希望这会有所帮助。