My error in Package Manager Console:
The property or navigation 'MenuId' cannot be added to the entity type 'ThAmCo.Catering.Models.FoodBooking' because a property or navigation with the same name already exists on entity type 'ThAmCo.Catering.Models.FoodBooking'.
I am trying to create a database for an assignment, however, I am stuck on this error.
This is my current DbContext:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ThAmCo.Catering.Models
{
public class ThAmCoContext : DbContext
{
public DbSet<MenuFoodItem> MenuFoodItem { get; set; }
public DbSet<FoodBooking > FoodBooking { get; set; }
public DbSet<Menu> Menu { get; set; }
public DbSet<FoodItem> FoodItem { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options) =>
options.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=ThAmCoCatering;");
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Foreign Key
builder.Entity<MenuFoodItem>()
.HasKey(a => new { a.MenuId, a.FoodItemId });
builder.Entity<MenuFoodItem>()
.HasOne(a => a.Menu)
.WithMany(m => m.FoodItem)
.HasForeignKey(a => a.MenuId)
.HasForeignKey(a => a.FoodItemId);
builder.Entity<MenuFoodItem>()
.HasOne(a => a.FoodItem)
.WithMany()
.HasForeignKey(a => a.FoodItemId);
builder.Entity<FoodBooking>()
.HasOne(m => m.Menu)
.WithMany()
.HasForeignKey(m => m.MenuId)
.OnDelete(DeleteBehavior.Restrict);
builder.Entity<FoodBooking>()
.HasOne(a => a.Menu)
.WithMany()
.HasForeignKey(a => a.MenuId);
}
}
}
And this is my FoodBooking:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;
namespace ThAmCo.Catering.Models
{
public class FoodBooking
{
public FoodBooking()
{
}
[Key]
public int FoodBookingId { get; set; }
public int ClientReferenceId { get; set; }
public int NumberOfGuests { get; set; }
public Menu Menu { get; set; }
public int MenuId { get; set; }
}
}
This is my FoodItem class:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace ThAmCo.Catering.Models
{
public class FoodItem
{
public FoodItem()
{
}
public FoodItem(int FoodItemId, string Description, float UnitPrice)
{
this.FoodItemId = FoodItemId;
this.Description = Description;
this.UnitPrice = UnitPrice;
}
public int FoodItemId { get; set; }
[Required]
public string Description { get; set; }
[Required]
public float UnitPrice { get; set; }
}
}
This is my Menu class:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace ThAmCo.Catering.Models
{
public class Menu
{
public Menu()
{
}
public Menu(int MenuId, string MenuName)
{
}
[Key]
public int MenuId { get; set; }
[Required]
public string MenuName { get; set; }
public ICollection<MenuFoodItem> FoodItem { get; set; }
}
}
This is my MenuFoodItem class:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace ThAmCo.Catering.Models
{
public class MenuFoodItem
{
public MenuFoodItem()
{
}
public MenuFoodItem(int FoodItemId, int MenuId)
{
this.FoodItemId = FoodItemId;
this.MenuId = MenuId;
}
public int FoodItemId { get; set; }
public FoodItem FoodItem { get; set; }
public int MenuId { get; set; }
public Menu Menu { get; set; }
}
}
For context it is supposed to be for a web app that allows me to create a menu and fooditems for a venue.
Here is what the ERD should look like according to specification:
CodePudding user response:
remove this duplicated part:
builder.Entity<FoodBooking>()
.HasOne(a => a.Menu)
.WithMany()
.HasForeignKey(a => a.MenuId);
update this part:
builder.Entity<MenuFoodItem>()
.HasOne(a => a.Menu)
.WithMany(m => m.FoodItem)
.HasForeignKey(a => a.MenuId)
.HasForeignKey(a => a.FoodItemId);
builder.Entity<MenuFoodItem>()
.HasOne(a => a.FoodItem)
.WithMany()
.HasForeignKey(a => a.FoodItemId);
to
builder.Entity<MenuFoodItem>()
.HasOne(a => a.FoodItem)
.WithMany()
.HasForeignKey(a => a.FoodItemId);
builder.Entity<MenuFoodItem>()
.HasOne(a => a.Menu)
.WithMany(a => a.FoodItem)
.HasForeignKey(a => a.MenuId);