我使用了 Microsoft.EntityFrameworkCore 版本 3.1.6 和 .net core sdk 3.1
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Hierarchy;
public class Item
{
public int ItemID{ get; set; }
[Key]
public HierarchyId ItemHID { get; set; }
}
此外,ItemHID 在 Item SQL 表中设置主键。构建成功,但我在运行时收到错误“System.InvalidOperationException:'实体类型'HierarchyId'需要定义主键。如果您打算使用无键实体类型,请调用'HasNoKey()'。 ’”。你有什么想法吗?
EF Core 8 现在原生支持 HierarchyID。
配置 DbContext 时添加以下配置。
options.UseSqlServer(
connectionString,
x => x.UseHierarchyId());