如何修复“无法添加实体类型“MenuFoodItem”的种子实体,因为没有为所需属性“FoodItemId1”提供值。”?

问题描述 投票:0回答:1

我正在开发一个网络应用程序和服务,专门从事带有菜单和预订的餐饮活动。我正在使用种子数据构建数据库,但是当我尝试进行初始创建时出现错误。

错误是:

无法添加实体类型“MenuFoodItem”的种子实体,因为没有为所需属性“FoodItemId1”提供值。

我的代码采用 C# 编写,分为 5 个类:

FoodBooking、FoodItem、Menu、MenuFoodItem 和 CateringDbContext。

这是我每个班级的代码:

餐饮DbContext:

using Microsoft.EntityFrameworkCore;
using static System.Net.Mime.MediaTypeNames;

namespace ThAmCo.Catering.Data
{
    public class CateringDbContext : DbContext
    {
        // Notes
        // - DbSet defines the database table.
        // - the class name is defined as part of the data model
        // - the instance/object name is normally plural
        // - by default, the instance name will become the table name

        public DbSet<Menu> Menus { get; set; }

        public DbSet<FoodItem> FoodItems { get; set; }

        public DbSet<MenuFoodItem> MenuFoodItems { get; set; }

        public DbSet<FoodBooking> FoodBookings { get; set; }

        private string DbPath { get; set; } = string.Empty;

        // Constructor to set-up the database path & name
        public CateringDbContext()
        {
            var folder = Environment.SpecialFolder.MyDocuments;
            var path = Environment.GetFolderPath(folder);
            DbPath = Path.Join(path, "ThAmCo.Catering.db");
        }
        // OnConfiguring to specify that the SQLite database engine will be used
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
            optionsBuilder.UseSqlite($"Data Source={DbPath}");
        }
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);


            builder.Entity<MenuFoodItem>()
                .HasKey(ms => new { ms.MenuId, ms.FoodItemId });


            builder.Entity<Menu>()
                .HasMany(m => m.MenuFoodItems)
                .WithOne(fi => fi.Menu)
                .HasForeignKey(tr => tr.FoodItemId)
                .OnDelete(DeleteBehavior.Restrict);

            builder.Entity<FoodItem>()
                .HasMany(m => m.MenuFoodItems)
                .WithOne(fi => fi.FoodItem)
                .OnDelete(DeleteBehavior.Restrict);


            // Seed/Test data to be added here

            builder.Entity<Menu>().HasData(
                new Menu { MenuId = 1, MenuName = "Pasta" },
                new Menu { MenuId = 2, MenuName = "Pizza" },
                new Menu { MenuId = 3, MenuName = "Pies" },
                new Menu { MenuId = 4, MenuName = "Carvery" },
                new Menu { MenuId = 5, MenuName = "Desert" }
                );

            builder.Entity<FoodItem>().HasData(
                new FoodItem { FoodItemId = 1, Description = "Tuna Pasta Bake", UnitPrice = 9.99F },
                new FoodItem { FoodItemId = 2, Description = "Four Cheese Pizza", UnitPrice = 10.99F },
                new FoodItem { FoodItemId = 3, Description = "Chicken and Leak Pie", UnitPrice = 5.65F },
                new FoodItem { FoodItemId = 4, Description = "Usual Sunday Carvery", UnitPrice = 12.99F },
                new FoodItem { FoodItemId = 5, Description = "Ice Cream with melted Cookies", UnitPrice = 6.99F }
                );

            builder.Entity<MenuFoodItem>().HasData(
                new MenuFoodItem { MenuId = 1, FoodItemId = 1 },
                new MenuFoodItem { MenuId = 2, FoodItemId = 2 },
                new MenuFoodItem { MenuId = 3, FoodItemId = 3 },
                new MenuFoodItem { MenuId = 4, FoodItemId = 4 },
                new MenuFoodItem { MenuId = 5, FoodItemId = 5 }
                );

            builder.Entity<FoodBooking>().HasData(
                new FoodBooking { FoodBookingId = 1, ClientReferenceId = 1, NumberOfGuests = 1, MenuId = 1 },
                new FoodBooking { FoodBookingId = 2, ClientReferenceId = 2, NumberOfGuests = 2, MenuId = 2 },
                new FoodBooking { FoodBookingId = 3, ClientReferenceId = 3, NumberOfGuests = 3, MenuId = 3 },
                new FoodBooking { FoodBookingId = 4, ClientReferenceId = 4, NumberOfGuests = 4, MenuId = 4 },
                new FoodBooking { FoodBookingId = 5, ClientReferenceId = 5, NumberOfGuests = 5, MenuId = 5 }
              );


        }

    }
}

餐饮预订:

using System.ComponentModel.DataAnnotations;

namespace ThAmCo.Catering.Data
{
    public class FoodBooking
    {
        [Required]
        public int FoodBookingId { get; set; }

        public int ClientReferenceId { get; set; }

        public int NumberOfGuests { get; set; }

        public int MenuId { get; set; }

        //Placeholder for link to FoodItem
        public FoodItem? FoodItem { get; set; }
    }
}

食品项目:

using System.ComponentModel.DataAnnotations;

namespace ThAmCo.Catering.Data
{
    public class FoodItem
    {
        [Key]

        public int FoodItemId { get; set; }

        [MaxLength(50)]
        public string Description { get; set; } = null!;

        public float UnitPrice { get; set; }

        // Placeholder for List of MenuFoodItem
        public List<MenuFoodItem> MenuFoodItems { get; set; }
       
        // Placeholder for List of FoodBooking
        public List<FoodBooking> FoodBookings { get; set; }
    }
}

菜单:

using System.ComponentModel.DataAnnotations;

namespace ThAmCo.Catering.Data
{
    public class Menu
    {
        [Key]


        [MaxLength(15)]
        public int MenuId { get; set; }

        [MaxLength(50)]
        public string MenuName { get; set; } = null!;



        //Placeholder for List of MenuFoodItem
        public List<MenuFoodItem> MenuFoodItems { get; set; }


    }
}

菜单食品项目:

using System.ComponentModel.DataAnnotations;

namespace ThAmCo.Catering.Data
{
    public class MenuFoodItem
    {
        [Required]
        public int MenuId { get; set; }
        
        [Required]
        [MaxLength(15)]
        public int FoodItemId { get; set; } 

        //Placeholder for link to FoodItem
        public FoodItem? FoodItem { get; set; }

        //Placeholder for link to Menu
        public Menu? Menu { get; set; }
    }
}

我相信我的人际关系在某个地方出了问题,但不能 100% 确定是哪一部分。

提前谢谢您。

c# asp.net sqlite entity-framework
1个回答
0
投票

问题可能在于使用

[MaxLength(15)]
作为
FoodItemId
属性,该属性应该表示整数值而不是字符串。这可能会导致冲突并解释错误消息。 请参阅https://learn.microsoft.com/en-us/dotnet/api/system.configuration.stringvalidatorattribute.maxlength?view=dotnet-plat-ext-7.0

using System.ComponentModel.DataAnnotations;

namespace ThAmCo.Catering.Data
{
    public class MenuFoodItem
    {
        [Required]
        public int MenuId { get; set; }

        [Required]
        public int FoodItemId { get; set; }

        public FoodItem? FoodItem { get; set; }

        public Menu? Menu { get; set; }
    }
}

您应该对 Menu 类执行相同的操作。

namespace ThAmCo.Catering.Data
{
    public class Menu
    {
        [Key]


        public int MenuId { get; set; }

        [MaxLength(50)]
        public string MenuName { get; set; } = null!;



        //Placeholder for List of MenuFoodItem
        public List<MenuFoodItem> MenuFoodItems { get; set; }


    }
}

如果您确实想使用对整数有效的最大值,则可以对整数使用

[MaxValue()]
属性。 https://learn.microsoft.com/en-us/dotnet/api/system.configuration.integervalidatorattribute.maxvalue?view=dotnet-plat-ext-7.0

© www.soinside.com 2019 - 2024. All rights reserved.