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 !!