【问题标题】:Why does Entity Framework Core generate HashSet?为什么 Entity Framework Core 会生成 HashSet?
【发布时间】:2021-08-28 00:16:19
【问题描述】:

如果我的实体与另一个实体没有关系,为什么 Entity Framework Core 会生成 HashSet

我收到以下错误

检测到不支持的可能对象循环。这可能是由于循环或对象深度大于最大允许深度 32

因为我的模型是循环的,我在导航属性中得到了 null。

我由 EF Core 创建的实体:

public partial class Customers
{
    public Customers()
    {
        Orders = new HashSet<Orders>();
    }

    public int CustomerId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Phone { get; set; }
    public string Email { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }

    public virtual ICollection<Orders> Orders { get; set; }
}

Orders实体:

public partial class Orders
{
    public Orders()
    {
        OrderItems = new HashSet<OrderItems>();
    }

    public int OrderId { get; set; }
    public int? CustomerId { get; set; }
    public byte OrderStatus { get; set; }
    public DateTime OrderDate { get; set; }
    public DateTime RequiredDate { get; set; }
    public DateTime? ShippedDate { get; set; }
    public int StoreId { get; set; }
    public int StaffId { get; set; }

    public virtual Customers Customer { get; set; }
    public virtual Staffs Staff { get; set; }
    public virtual Stores Store { get; set; }
    public virtual ICollection<OrderItems> OrderItems { get; set; }
}

我的数据库架构:

CREATE TABLE [sales].[customers]
(
    [customer_id] [int] IDENTITY(1,1) NOT NULL,
    [first_name] [varchar](255) NOT NULL,
    [last_name] [varchar](255) NOT NULL,
    [phone] [varchar](25) NULL,
    [email] [varchar](255) NOT NULL,
    [street] [varchar](255) NULL,
    [city] [varchar](50) NULL,
    [state] [varchar](25) NULL,
    [zip_code] [varchar](5) NULL,

    PRIMARY KEY CLUSTERED ([customer_id] ASC)
                WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, 
                      IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, 
                      ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

CREATE TABLE [sales].[orders]
(
    [order_id] [int] IDENTITY(1,1) NOT NULL,
    [customer_id] [int] NULL,
    [order_status] [tinyint] NOT NULL,
    [order_date] [date] NOT NULL,
    [required_date] [date] NOT NULL,
    [shipped_date] [date] NULL,
    [store_id] [int] NOT NULL,
    [staff_id] [int] NOT NULL,

    PRIMARY KEY CLUSTERED ([order_id] ASC)
                WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, 
                      IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, 
                      ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

ALTER TABLE [sales].[orders] WITH CHECK 
    ADD FOREIGN KEY([customer_id])
        REFERENCES [sales].[customers] ([customer_id])
        ON UPDATE CASCADE
        ON DELETE CASCADE
GO

ALTER TABLE [sales].[orders] WITH CHECK 
    ADD FOREIGN KEY([staff_id])
        REFERENCES [sales].[staffs] ([staff_id])
GO

ALTER TABLE [sales].[orders] WITH CHECK 
    ADD FOREIGN KEY([store_id])
        REFERENCES [sales].[stores] ([store_id])
        ON UPDATE CASCADE
        ON DELETE CASCADE
GO

【问题讨论】:

  • 我换了班级图片
  • @DaleK 我上传了 DLL
  • @DaleK lo siento amigo,你没有 sabia como aceptar sus cambios。 He corregido nuevamente。
  • if my entity has no relation to another entity. 你的问题清楚地表明 is 关系/FK。
  • 但是为什么客户有一个ICollection Orders 如果关系对客户有订单。一个订单只有一个客户。但是在 EF Core 创建的模型中,客户端可以有很多订单。实际上,该自动生成的模型对我不起作用,因为它为这么多嵌套对象生成异常,并且它不是真正的模型,因为它在数据库中创建了我的关系。

标签: c# sql-server api entity-framework entity-framework-core


【解决方案1】:
  • 构造函数中的 HashSet 不是强制性的,它旨在帮助您在没有从数据库中获取记录时避免 NullReferenceExceptions
  • 您可以使用其他 Collection 类型,但我相信 HashSet 在大多数情况下是合适的。

【讨论】:

  • 我不明白的是,如果在我的数据库模型中没有这样指示,EF Core 会创建一个属性“ICollection Orders”。
  • 如果打算让表Customers和表Orders之间的关系是一一对应的,还必须在表Custo中​​注明FK列order_id
  • 现在查看您的架构,我认为表 Customers 和 Orders 之间的关系是一对多的,这就是 EF Core 自动生成字段 public virtual ICollection Orders
  • 我想我明白了。我的数据库方法是正确的。但是有没有办法告诉EF不要设置引用“一对多”关系的集合(一个客户端可以有多个订单)。不手动删除可以吗?
  • 使用以下指令自动创建实体 "dotnet ef dbcontext scaffold" MY_CON_STRING "Microsoft.EntityFrameworkCore.SqlServer --output-dir Entities \ Test --schema production --schema sales --context-dir Repositories --context SijDbPrueba"
【解决方案2】:

错误是由于循环引用的问题,这是我使用 Asp Net Core 5 的项目的解决方案,忽略循环引用。

致谢:https://github.com/dotnet/aspnetcore/issues/28286

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-07
    • 2018-03-20
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    • 1970-01-01
    • 2021-06-11
    • 1970-01-01
    相关资源
    最近更新 更多