1

I got 3 tables,

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

        public int ParkCityId { get; set; }

        public int ParkStreetId { 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; }

    }

The idea is - Cities got many Streets, Streets got many Parkings. The goal in the end is to bring a Parkings object that contains:

[dbo.Parkings.ParkId, dbo.Parkings.ParkStartDate, dbo.Parkings.ParkEndDate, dbo.Cities.CityDesc, dbo.Streets.StreetDesc]

(And the form to enter a new parking will contain: StartDate, EndDate,CityDesc,StreetDesc)

I'm trying first to seed the database with data, in this class:

 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.Parkings.Add(
              new Parkings() { ParkStreetId = 1, ParkCityId = 1, StartDate = "2018-01-17", EndDate = "2018-01-17" }
              );

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

            context.SaveChanges();
        }
    }

But i get this error:

"exceptionMessage": "The ForeignKeyAttribute on property 'Cities' on type 'BeraleBack.Models.entites+Parkings' is not valid. The foreign key name 'CityId' was not found on the dependent type 'BeraleBack.Models.entites+Parkings'. The Name value should be a comma separated list of foreign key property names.",
  "exceptionType": "System.InvalidOperationException",

I would like to know first, if my configuration is right based on the info i gave. Second, how should i seed it?

I'm a complete noob in it and didnt met any helpful tutorial..

Thanks a lot !!

Aa Yy
  • 1,702
  • 5
  • 19
  • 34
  • By seed do you mean seed it with data or just create the database? – Andrew Jan 17 '18 at 17:49
  • Seed it with data @Andrew – Aa Yy Jan 17 '18 at 17:53
  • Can we seed your Seed method? Like listed here: https://learn.microsoft.com/en-us/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-3 – Andrew Jan 17 '18 at 17:54
  • Lots of issues here that may require separate questions. Your current error is because you have a typo on your FK, it should be `[ForeignKey("ParkCityId")]` not `[ForeignKey("CityId")]`. The next issue you will probably run into is that your keys are indentity so you can't seed them that way. See [here](https://stackoverflow.com/questions/24265300/identity-insert-during-seeding-with-entityframework-6-code-first). – Steve Greene Jan 17 '18 at 18:18
  • why? `[ForeignKey("CityId")]` is directing to dbo.Cities. not dbo.Parkings – Aa Yy Jan 18 '18 at 10:04

1 Answers1

0

Bear with me as I work with DBFirst more often now but it looks like you entity framework cannot find your foreign key property.

Compare Parkings to Streets

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

        public int CityId { get; set; } //Property which foreign key is assoctiated with
        [Required]
        public string StreetDesc { get; set; }

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

    }

But you don't have this property in the Parkings entity

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

        public int ParkStreetId { get; set; }

        public int CityId{ get; set; }

        [ForeignKey("CityId")]
        public virtual Cities Cities { get; set; }
    }
Andrew
  • 720
  • 3
  • 9
  • 34
  • But can you sort a little thing for me, now if i understand right, parkings is configured to work with cities, but it also needs a relation with streets. how should i configure a foreign key for it too, without messing the whole thing? – Aa Yy Jan 18 '18 at 10:11
  • simply adding `[ForeignKey("StreetId")] public virtual Streets Streets { get; set; }` to dbo.Parkings gives en error: `Introducing FOREIGN KEY constraint 'FK_dbo.Streets_dbo.Cities_CityId' on table 'Streets' may cause cycles or multiple cascade paths` – Aa Yy Jan 18 '18 at 10:14