【发布时间】:2022-01-26 17:44:50
【问题描述】:
我在包管理器控制台中的错误:
无法将属性或导航“MenuId”添加到实体类型“ThAmCo.Catering.Models.FoodBooking”,因为实体类型“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);
}
}
}
这是我的 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; }
}
}
这是我的 FoodItem 类:
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; }
}
}
这是我的菜单类:
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 类:
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; }
}
}
对于上下文,它应该是一个网络应用程序,它允许我为一个场所创建菜单和食物。
【问题讨论】:
-
安装 EF Core Power Tools 并在现有数据库上调用其逆向工程师选项(或者,对于更有限的手动版本,请从命令行使用 Scaffold-DbContext)。这是获取为您拥有的数据库正确设置的 DbContext 和一组实体的最快、最简单的方法(如果您还没有数据库,请在 SSMS 中创建它;它比让 EF 创建它更快、更容易给你)
-
因为这是一个任务,我不确定这是否被允许,我总是可以试一试。
-
看了一下,Power Tools 无法工作,因为我使用的是 3.1.19 EF 并且不受支持
-
我刚才测试的时候没有问题-在netcore 31上做了一个新项目,安装了EF 3.1.19并使用EFCPT反转了一个db,将EF版本设置为Core 3-为我工作..
标签: c# entity-framework entity-framework-core