Witam
Może i późna godzina ale tak męczy mnie taka rzecz, której o dziwo nie mogę znaleźć w googlu. Mam encję:
public class Produkt {
public int Id [ get; set; }
public string ProductName { get; set; }
[Required]
public virtual User { get; set; }
public int UserId { get; set; }
}
public class User {
int Id { get; set; }
string Name { get; set; }
}
Userzy w bazie istnieją, zatem nie ściągam sobie całego ale tylko Id przechowuje. I teraz chcę dodać nowy produkt.
int userId = 111;
var produkt = new Product { ProductName = "01", UserId = userId };
appContext.Produkty.Add(produkt);
appContext.SaveChanges()
W tym momencie wywala mi błąd, że brakuje mu pola User, które jest required. Ktoś wie jak rozwiązać ten problem?