简体   繁体   English

实体类型“列表<string> ' 需要定义主键</string>

[英]The entity type 'List<string>' requires a primary key to be defined

Getting "The entity type List<string'> requires a primary key to be defined."获取“实体类型 List<string'> 需要定义主键。” using .NET 6 to build a Web API.使用 .NET 6 构建一个 Web API。

The following is my Model class defining "Sales":以下是我的 Model class 定义“销售”:

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace SalesAPI.Data
{
    public class SalesItem
    {
     
        [Key]
        [Required]
        public Guid UserID { get; set; }

        [Required]
        [MinLength(5)]
        [MaxLength(75)]
        public String Title { get; set; }  = String.Empty;

        [Required]
        [MinLength(5)]
        public String Description { get; set; } = String.Empty;

        public List<String> Images { get; set; } = new List<String>();

        [Required]
        public DateTime ListingTime { get; set; }

        public String Location { get; set; } = String.Empty;

        public String ContactInfo { get; set; } = String.Empty;
    }
}

The following is my DBContext class:下面是我的DBContext class:

using Microsoft.EntityFrameworkCore;
using SalesAPI.Controllers;
    
namespace SalesAPI.Data
{
    public class DataContext : DbContext
    {
        public DataContext(DbContextOptions<DataContext> options) : base(options) { }

        public DbSet<SalesItem> SalesItems { get; set; }
        
    }
}

You'll have to make sure you have a table for the你必须确保你有一张桌子

public List<String> Images { get; set; } = new List<String>

since the database isn't able to reference an unknown list size in the table created.因为数据库无法在创建的表中引用未知的列表大小。

Change改变

public List<String> Images { get; set; } = new List<String>

To

public List<ImageUri> Images { get; set; } = new List<ImageUri>

and create a class.并创建一个类。

public class ImageUri
{
    [Key]
    public int Id { get; set; }
    public string Uri { get; set; } = null!;
}

You can convert the List into a delimited string and use it that way instead of having to create a special class to basically hold a string.您可以将 List 转换为带分隔符的字符串并以这种方式使用它,而不必创建一个特殊的 class 来基本上保存一个字符串。

modelBuilder.Entity<Berk>(x =>
{
    x.HasKey(y => y.BerkId);
    x.Property(y => y.TheList)
        .HasConversion(
            from => string.Join(";", from),
            to => string.IsNullOrEmpty(to) ? new List<string>() : to.Split(';', StringSplitOptions.RemoveEmptyEntries).ToList(),
            new ValueComparer<List<string>>(
                (c1, c2) => c1.SequenceEqual(c2),
                c => c.Aggregate(0, (a, v) => HashCode.Combine(a, v.GetHashCode())),
                c => c.ToList()
        )
    );
});

public class Berk
{
    [Key]
    public int BerkId { get; set; }
    public List<string> TheList { get; set; }
}

The HasConversion has the value stored as a delimited string in the db, splits it to the List when you are using it and then joins it back again when saving. HasConversion 将值作为分隔字符串存储在数据库中,在您使用它时将其拆分到列表中,然后在保存时再次将其重新加入。 Just pick a delimiter that will not be in any of your strings.只需选择一个不会出现在您的任何字符串中的定界符。

This was bugging me this afternoon, hope it helps someone else.今天下午这让我很烦恼,希望它能帮助别人。

We encountered this issue when extending an EF generated model via partial class. The partial class included two properties that were not reflective of the underlying data.table and generated the error: "entity type 'list' requires a primary key to be defined".我们在通过部分 class 扩展生成 model 的 EF 时遇到了这个问题。部分 class 包含两个不能反映基础 data.table 的属性,并生成错误:“实体类型‘列表’需要定义主键”。 In the end, our solution was to include the [NotMapped] attribute as follows:最后,我们的解决方案是包含 [NotMapped] 属性,如下所示:

 [NotMapped] public List<string> Roles { get; set; } = new List<string>(); [NotMapped] public List<string[]> Claims { get; set; } = new List<string[]>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM