Luciferno
Luciferno
CC#
Created by Luciferno on 11/9/2022 in #help
Value binding
4 replies
CC#
Created by Luciferno on 10/19/2022 in #help
EF Core many-to-many relation error
6 replies
CC#
Created by Luciferno on 9/21/2022 in #help
How to keep track of third property in Many-To-Many
I have 2 models one that is Reservation and one that is Menu, They have a many-to-many relation but I wanna keep track on the reservation how many times one menu is ordered for that resrevation. This is my current many-to-many table :
public int ReservationId { get; set; }
public Reservation Reservation { get; set; }

public int MenuId { get; set; }
public int TimesOrdered { get; set; } <-- this property needs to keep track of how many times a menu has been ordered.
public Menu Menu { get; set; }
public int ReservationId { get; set; }
public Reservation Reservation { get; set; }

public int MenuId { get; set; }
public int TimesOrdered { get; set; } <-- this property needs to keep track of how many times a menu has been ordered.
public Menu Menu { get; set; }
This is my current OnModelCreating for that table
modelBuilder.Entity<Reservation>()
.HasMany(x => x.Menus)
.WithMany(x => x.Reservations)
.UsingEntity<ReservationMenu>(
rm => rm.HasOne(x => x.Menu).WithMany().HasForeignKey(k => k.MenuId),
rm => rm.HasOne(x => x.Reservation).WithMany().HasForeignKey(k => k.ReservationId),
rm =>
{
rm.HasKey(p => new { p.MenuId, p.ReservationId });
}
);
modelBuilder.Entity<Reservation>()
.HasMany(x => x.Menus)
.WithMany(x => x.Reservations)
.UsingEntity<ReservationMenu>(
rm => rm.HasOne(x => x.Menu).WithMany().HasForeignKey(k => k.MenuId),
rm => rm.HasOne(x => x.Reservation).WithMany().HasForeignKey(k => k.ReservationId),
rm =>
{
rm.HasKey(p => new { p.MenuId, p.ReservationId });
}
);
I am kinda stuck on how to approach this.
4 replies
CC#
Created by Luciferno on 9/18/2022 in #help
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
64 replies
CC#
Created by Luciferno on 9/14/2022 in #help
Deserialization Error
9 replies