Kol*_*256 3 c# entity-framework-core
获取“实体类型 List<string'> 需要定义主键。” 使用 .NET 6 构建 Web API。
以下是我定义“销售”的模型类:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
以下是我的 DBContext 类:
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; }
}
}
Run Code Online (Sandbox Code Playgroud)
小智 7
我们在通过分部类扩展 EF 生成的模型时遇到了这个问题。分部类包含两个不反映基础数据表的属性并生成错误:
实体类型“列表”需要定义主键”。
最后,我们的解决方案是包含该[NotMapped]属性,如下所示:
[NotMapped]
public List<string> Roles { get; set; } = new List<string>();
[NotMapped]
public List<string[]> Claims { get; set; } = new List<string[]>();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10393 次 |
| 最近记录: |