没有给出对应于 xxx 所需形式参数“id"的参数 [英] There is no argument given that corresponds to the required formal parameter 'id' of xxx

查看:22
本文介绍了没有给出对应于 xxx 所需形式参数“id"的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实例化这个类:

I'm trying to instantiate this class:

班级:

using System.Text.Json.Serialization;

public class GroceryItemTag
{
    [JsonConstructor]
    public GroceryItemTag(string name, GroceryItemTagEnum id, int iconCodePoint)
    {
        Id = id;
        IconCodePoint = iconCodePoint;
        Name = name;
    }

    public GroceryItemTagEnum Id { get; set; }
    public int IconCodePoint { get; set; }
    public string Name { get; set; }
}

我收到此错误:

There is no argument given that corresponds to the required formal parameter 'id' of 'GroceryItemTag.GroceryItemTag(GroceryItemTagEnum, int, string)' 

这里我试图实例化类(枚举是整数):

Here I am trying to instantiate the class (the enums are ints):

    builder.Entity<GroceryItemTag>().HasData(
        new GroceryItemTag[] {
        new GroceryItemTag(
            "Baby & Child",
            GroceryItemTagEnum.BabyAndChild,
            0xf77c
        ),
        new GroceryItemTag(
            "Baking",
            GroceryItemTagEnum.Baking,
            0xf563
        ),...

GroceryItemTagEnum:

GroceryItemTagEnum:

public enum GroceryItemTagEnum
{
  BabyAndChild = 1,
  Baking,
  Bathroom,
  BeerAndWine,
  Condiments,
  Confectionary,  
  Cooking,
  Dessert,
  Drinks,
  FauxDairy,
  FauxMeat,
  FauxSeafood,
  FridgeAndDeli,
  Frozen,
  HealthFood,
  HouseHold,
  Other,
  Pantry,
  Pet,
}     

数据库上下文:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;

namespace Vepo.Domain
{
    public class VepoContext : DbContext
    {
        public VepoContext(DbContextOptions<VepoContext> options)
            : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            builder.Entity<GroceryItemGroceryStore>().HasKey(table => new
            {
                table.GroceryItemId,
                table.GroceryStoreId
            });

            builder.Entity<MenuItemRestaurant>().HasKey(table => new
            {
                table.MenuItemId,
                table.RestaurantId
            });

            builder.Entity<GroceryItemTag>()
            .Property(tag => tag.Id)
            .ValueGeneratedNever();

            builder.Entity<MenuItemTag>()
            .Property(tag => tag.Id)
            .ValueGeneratedNever();

            builder.Entity<GroceryItemTag>().HasData(
                new GroceryItemTag[] {
                new GroceryItemTag(
                    "Baby & Child",
                    GroceryItemTagEnum.BabyAndChild,
                    0xf77c
                ),
                new GroceryItemTag(
                    "Baking",
                    GroceryItemTagEnum.Baking,
                    0xf563
                ),
                new GroceryItemTag (
                    "Beer & Wine",
                    GroceryItemTagEnum.BeerAndWine,
                    0xf4e3
                ),
                new GroceryItemTag (
                    "Condiments",
                    GroceryItemTagEnum.Condiments,
                    0xf72f
                ),
                new GroceryItemTag (
                    "Confectionary",
                    GroceryItemTagEnum.Confectionary,
                    0xf819
                ),
                new GroceryItemTag (
                    "Cooking",
                    GroceryItemTagEnum.Cooking,
                    0xe01d
                ),
                new GroceryItemTag (
                    "Dessert",
                    GroceryItemTagEnum.Dessert,
                    0xf810
                ),
                new GroceryItemTag (
                    "Drinks",
                    GroceryItemTagEnum.Drinks,
                    0xf804
                ),
                new GroceryItemTag (
                    "Faux Meat",
                    GroceryItemTagEnum.FauxMeat,
                    0xf814
                ),
                new GroceryItemTag (
                    "Faux Dairy",
                    GroceryItemTagEnum.FauxDairy,
                    0xf7f0
                ),
                new GroceryItemTag (
                    "Faux Seafood",
                    GroceryItemTagEnum.FauxSeafood,
                    0xf7fe
                ),
                new GroceryItemTag (
                    "Fridge & Deli",
                    GroceryItemTagEnum.FridgeAndDeli,
                    0xe026
                ),
                new GroceryItemTag (
                    "Frozen",
                    GroceryItemTagEnum.Frozen,
                    0xf7ad
                ),
                new GroceryItemTag (
                    "Bathroom",
                    GroceryItemTagEnum.Bathroom,
                    0xe06b
                ),
                new GroceryItemTag (
                    "Health Food",
                    GroceryItemTagEnum.HealthFood,
                    0xf787
                ),
                new GroceryItemTag (
                    "Household",
                    GroceryItemTagEnum.HouseHold,
                    0xf898
                ),
                new GroceryItemTag (
                    "Pantry",
                    GroceryItemTagEnum.Pantry,
                    0xf7eb
                ),
                new GroceryItemTag (
                    "Pet",
                    GroceryItemTagEnum.Pet,
                    0xf6d3
                ),
                new GroceryItemTag (
                    "Other",
                    GroceryItemTagEnum.Other,
                    0xf39b
                }});

            builder.Entity<MenuItemTag>().HasData(
                new MenuItemTag[] {
                new MenuItemTag {
                    Name = "American",
                    Id = MenuItemTagEnum.American,
                    IconCodePoint = 0xf803
                },
                new MenuItemTag {
                    Name = "Asian",
                    Id = MenuItemTagEnum.Asian,
                    IconCodePoint = 0xf823
                },
                new MenuItemTag {
                    Name = "Bakery",
                    Id = MenuItemTagEnum.Bakery,
                    IconCodePoint = 0xf705
                },
                new MenuItemTag {
                    Name = "Barbecue",
                    Id = MenuItemTagEnum.Barbecue,
                    IconCodePoint = 0xf80f
                },
                new MenuItemTag {
                    Name = "Breakfast",
                    Id = MenuItemTagEnum.Breakfast,
                    IconCodePoint = 0xe002
                },
                new MenuItemTag {
                    Name = "Cafe",
                    Id = MenuItemTagEnum.Cafe,
                    IconCodePoint = 0xf6c5
                },
                new MenuItemTag {
                    Name = "Chinese",
                    Id = MenuItemTagEnum.Chinese,
                    IconCodePoint = 0xf823
                },
                new MenuItemTag {
                    Name = "Deli",
                    Id = MenuItemTagEnum.Deli,
                    IconCodePoint = 0xf81f
                },
                new MenuItemTag {
                    Name = "Desserts",
                    Id = MenuItemTagEnum.Desserts,
                    IconCodePoint = 0xf551
                },
                new MenuItemTag {
                    Name = "European",
                    Id = MenuItemTagEnum.European,
                    IconCodePoint = 0xf7a2
                },
                new MenuItemTag {
                    Name = "Fish & Chips",
                    Id = MenuItemTagEnum.FishAndChips,
                    IconCodePoint = 0xf7fe
                },
                new MenuItemTag {
                    Name = "Indian",
                    Id = MenuItemTagEnum.Indian,
                    IconCodePoint = 0xf156
                },
                new MenuItemTag {
                    Name = "French",
                    Id = MenuItemTagEnum.Fench,
                    IconCodePoint = 0xf7f6
                },
                new MenuItemTag {
                    Name = "German",
                    Id = MenuItemTagEnum.German,
                    IconCodePoint = 0xf820
                },
                new MenuItemTag {
                    Name = "Greek",
                    Id = MenuItemTagEnum.Greek,
                    IconCodePoint = 0xf68b
                },
                new MenuItemTag {
                    Name = "Health Food",
                    Id = MenuItemTagEnum.HealthFood,
                    IconCodePoint = 0xf81e
                },
                new MenuItemTag {
                    Name = "Italian",
                    Id = MenuItemTagEnum.Italian,
                    IconCodePoint = 0xf817
                },
                new MenuItemTag {
                    Name = "Japanese",
                    Id = MenuItemTagEnum.Japanese,
                    IconCodePoint = 0xf56a
                },
                new MenuItemTag {
                    Name = "Kebab",
                    Id = MenuItemTagEnum.Kebab,
                    IconCodePoint = 0xf821
                },
                new MenuItemTag {
                    Name = "Kiwi",
                    Id = MenuItemTagEnum.Kiwi,
                    IconCodePoint = 0xf535
                },
                new MenuItemTag {
                    Name = "Korean",
                    Id = MenuItemTagEnum.Korean,
                    IconCodePoint = 0xf159
                }});


            builder.Entity<GroceryItem>()
            .Property(e => e.Tags)
            .HasConversion(
                v => JsonSerializer.Serialize(v, null),
                v => JsonSerializer.Deserialize<List<GroceryItemTag>>(v, null),
                new ValueComparer<IList<GroceryItemTag>>(
                    (c1, c2) => c1.SequenceEqual(c2),
                    c => c.Aggregate(0, (a, v) => HashCode.Combine(a, v.GetHashCode())),
                    c => (IList<GroceryItemTag>)c.ToList()));

        }

        public DbSet<GroceryItem> GroceryItems { get; set; }
        public DbSet<GroceryItemGroceryStore> GroceryItemGroceryStores { get; set; }
        public DbSet<MenuItemRestaurant> MenuItemRestaurants { get; set; }
        public DbSet<MenuItem> MenuItems { get; set; }
        public DbSet<GroceryStore> GroceryStores { get; set; }
        public DbSet<GroceryItemTag> GroceryItemTags { get; set; }
        public DbSet<MenuItemTag> MenuItemTags { get; set; }
        public DbSet<Restaurant> Restaurants { get; set; }
    }
}

单击转到定义会转到相同的 GroceryItemTag.以前是这样的:

Clicking go to definition does go to the same GroceryItemTag. It used to be like this:

public class GroceryItemTag
{
    public GroceryItemTagEnum Id { get; set; }
    public int IconCodePoint {get; set;}
    public string Name { get; set; }
}

但我改变了它.

悬停错误:

为什么我会收到此错误?

Why am I getting this error?

尝试完全限定名称:

我将 GroceryItemTag 添加到 命名空间 Vepo.Domain.现在,当我单击转到 GroceryItemTag 上的定义时,它会将我带到 GroceryItemTag 的构造函数.然后转到 GRoceryItemTag 的定义.GroceryItemTag 说没有找到定义.

I added GroceryItemTag to namespace Vepo.Domain. Now when I click go to definition on GroceryItemTag it takes me to the constructor of GroceryItemTag. And go to definition of GRoceryItemTag.GroceryItemTag says no definition found.

推荐答案

new GroceryItemTag中右键点击GroceryItemTag,然后Go To Defintion并确保 VS 已解析出与您的 JsonConstructor 匹配的正确定义.

Right click on GroceryItemTag in new GroceryItemTag, then Go To Defintion and make sure that VS has resolved the correct definition matching your JsonConstructor.

这种类型的错误看起来像是命名空间问题,但无论如何,您的代码对编译器来说显然是不明确的.

This type of error looks like a namespace issue, but eitherway your code is clearly ambigous to the compiler.

还可以尝试在 new 表达式中使用完全限定的类型名称,这可能有助于识别冲突.

Also try using the fully qualifed type name in your new expression, it might help identify the conflict.

builder.Entity<GroceryItemTag>().HasData(
        new Vepo.Domain.GroceryItemTag[] {
        new Vepo.Domain.GroceryItemTag(
            "Baby & Child",
            GroceryItemTagEnum.BabyAndChild,
            0xf77c
        ),
        new Vepo.Domain.GroceryItemTag(
            "Baking",
            GroceryItemTagEnum.Baking,
            0xf563
        ),...


因为你说的是​​c#5,所以我想说它和模式匹配有关系,但我无法复制.


Because you said c# 5, I want to say it has something to do with pattern matching, but I cannot replicate it.

这篇关于没有给出对应于 xxx 所需形式参数“id"的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆