Hej, dla zabawy robię sobię prosty sklep internetowy w asp.net, wszystko działało do momentu w którym chciałem dodać możliwość komentowania i oceny produktów. Dodałem klasę za to odpowiedzialną i w rezultacie mój model wygląda tak:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public string Category { get; set; }
public string Description { get; set; }
public int Price { get; set; }
public virtual Picture Picture { get; set; }
public virtual IList<ProductComentAndRate> ProductCommentsAndRates { get; set; }
}
public class Picture
{
public int Id { get; set; }
public string Patch { get; set; }
public string Extension { get; set; }
public string MoreInformation { get; set; }
}
public class ProductComentAndRate
{
public int Id { get; set; }
[Required(ErrorMessage="Write here your name or pseudoname")]
public string Author { get; set; }
[Required(ErrorMessage="Write number from 1 to 10")]
[Range(1, 10)]
public int Rate { get; set; }
[Required(ErrorMessage="Write here you opinion")]
public string Opinion { get; set; }
public DateTime PostDate { get; set; }
}
Komentarze się dodają i wszystko wygląda dobrze, ale gdy próbuję usunąć jakiś produkt pojawia się błąd. W komentarzu błędów pisze coś o braku klucza obcego. Faktycznie w tabeli Products brakuje klucza obcego dla tabeli ProductComentAndRates, chociaż dla tabeli Pictures klucz obcy jest. W tabeli ProductComentAndRate jest natomiast klucz tabeli Products. Co robię nie tak? Pozdrawiam.