属性 已经存在,无法添加?
Property cannot be added as it already exists?
我在程序包管理器控制台中的错误:
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'.
我正在尝试为作业创建数据库,但是,我遇到了这个错误。
这是我当前的 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);
}
}
}
这是我的美食预订:
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; }
}
}
这是我的 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; }
}
}
这是我的菜单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; }
}
}
这是我的 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; }
}
}
就上下文而言,它应该是一个网络应用程序,它允许我为一个场所创建菜单和食品。
根据规范,ERD 应如下所示:
删除这个重复的部分:
builder.Entity<FoodBooking>()
.HasOne(a => a.Menu)
.WithMany()
.HasForeignKey(a => a.MenuId);
更新这部分:
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<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);
我在程序包管理器控制台中的错误:
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'.
我正在尝试为作业创建数据库,但是,我遇到了这个错误。
这是我当前的 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);
}
}
}
这是我的美食预订:
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; }
}
}
这是我的 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; }
}
}
这是我的菜单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; }
}
}
这是我的 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; }
}
}
就上下文而言,它应该是一个网络应用程序,它允许我为一个场所创建菜单和食品。
根据规范,ERD 应如下所示:
删除这个重复的部分:
builder.Entity<FoodBooking>()
.HasOne(a => a.Menu)
.WithMany()
.HasForeignKey(a => a.MenuId);
更新这部分:
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<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);