【发布时间】: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