C
C#•3y ago
Luciferno

EF-Core data reference [Answered]

So for school we have to make a Restaurant management system. Now I am working on Reservations with the following model
[Key]
public int ReservationID { get; set; }

public string? FirstName { get; set; }

public string? LastName { get; set; }

public string? Adress { get; set; }

public string? City { get; set; }

public string? Email { get; set; }

public int GuestsComing { get; set; }

public DateTime? Date { get; set; }

public TimeSpan? Time { get; set; }

public List<Menu> ChoosenMenus { get; set; }

public decimal FoodPrice { get; set; }

public int UserFK { get; set; }
[Key]
public int ReservationID { get; set; }

public string? FirstName { get; set; }

public string? LastName { get; set; }

public string? Adress { get; set; }

public string? City { get; set; }

public string? Email { get; set; }

public int GuestsComing { get; set; }

public DateTime? Date { get; set; }

public TimeSpan? Time { get; set; }

public List<Menu> ChoosenMenus { get; set; }

public decimal FoodPrice { get; set; }

public int UserFK { get; set; }
But the problem I encounter is that the list ChoosenMenus exists of Menus that are already saved in my database but when I make a post request it still tries to add the menu's This is my DbContext regarding the Reservation model so far
modelBuilder.Entity<Reservation>()
.HasOne<UserInfo>()
.WithMany()
.HasForeignKey(x => x.UserFK);
modelBuilder.Entity<Reservation>()
.HasOne<UserInfo>()
.WithMany()
.HasForeignKey(x => x.UserFK);
So I somehow need to make a reference to the already exisiting menus instead of adding them but I dont really know how to
33 Replies
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Luciferno
LucifernoOP•3y ago
Yes, and save that with the Reservation
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Luciferno
LucifernoOP•3y ago
Well thats it I just get a error back from swagger, one second im gonna make a screen shot of that error Huh, I have a diffrent error now give me one sec
Luciferno
LucifernoOP•3y ago
So this is the error I get. and this is the Json that im posting
{
"ReservationID":0,
"FirstName":"test",
"LastName":"test",
"Adress":"test",
"City":"test",
"Email":"[email protected]",
"GuestsComing":4,
"Date":"2022-09-18T00:00:00",
"Time":"18:00:00",
"ChoosenMenus":[ <--- These menus already exist in the
{
"MenuId":1,
"MenuName":"Test Menu",
"MenuPrice":2.00,
"TimesOrdered":3,
"Dishes":[

]
}
],
"FoodPrice":2.00,
"UserFK":1
}
{
"ReservationID":0,
"FirstName":"test",
"LastName":"test",
"Adress":"test",
"City":"test",
"Email":"[email protected]",
"GuestsComing":4,
"Date":"2022-09-18T00:00:00",
"Time":"18:00:00",
"ChoosenMenus":[ <--- These menus already exist in the
{
"MenuId":1,
"MenuName":"Test Menu",
"MenuPrice":2.00,
"TimesOrdered":3,
"Dishes":[

]
}
],
"FoodPrice":2.00,
"UserFK":1
}
Luciferno
LucifernoOP•3y ago
But if I turn off the IDENTITY_INSET option it will add that menu again to my Menus table I guess? This is my function that adds the Reservation to the DB
public void AddReservation(Reservation reservation)
{
_dbContext.Reservations.Add(reservation);
_dbContext.SaveChanges();
}
public void AddReservation(Reservation reservation)
{
_dbContext.Reservations.Add(reservation);
_dbContext.SaveChanges();
}
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Luciferno
LucifernoOP•3y ago
Yea but i was wondering how to do that, since u cant save an property of
public int[] menuIds { get; set; }

or

public List<int> menuIds { get; set; }
public int[] menuIds { get; set; }

or

public List<int> menuIds { get; set; }
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Luciferno
LucifernoOP•3y ago
Ah I see
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Luciferno
LucifernoOP•3y ago
Yea true
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Luciferno
LucifernoOP•3y ago
Yea we don't want that haha
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Luciferno
LucifernoOP•3y ago
Nah i have blazor as front end
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Luciferno
LucifernoOP•3y ago
Yea i have JWT token sorted
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Luciferno
LucifernoOP•3y ago
ye
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Luciferno
LucifernoOP•3y ago
true!
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX•3y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Luciferno
LucifernoOP•3y ago
public int ReservationID { get; set; }

public string? FirstName { get; set; }

public string? LastName { get; set; }

public string? Adress { get; set; }

public string? City { get; set; }

public string? Email { get; set; }

public int GuestsComing { get; set; }

public DateTime? Date { get; set; }

public TimeSpan? Time { get; set; }

public List<Menu> ChoosenMenus { get; set; }

public decimal FoodPrice { get; set; }

public int? UserFK { get; set; }
public int ReservationID { get; set; }

public string? FirstName { get; set; }

public string? LastName { get; set; }

public string? Adress { get; set; }

public string? City { get; set; }

public string? Email { get; set; }

public int GuestsComing { get; set; }

public DateTime? Date { get; set; }

public TimeSpan? Time { get; set; }

public List<Menu> ChoosenMenus { get; set; }

public decimal FoodPrice { get; set; }

public int? UserFK { get; set; }
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Luciferno
LucifernoOP•3y ago
yea that seems better
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Luciferno
LucifernoOP•3y ago
Okay I will do that!
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Luciferno
LucifernoOP•3y ago
Thanks 🙂
Accord
Accord•3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server