【发布时间】:2026-01-25 11:40:01
【问题描述】:
我的 DbContext 似乎忽略了我对特定实体的流畅配置。它在OUTER JOIN 的子查询中生成表SubRegionRegions,当我清楚地在流利的配置中指定了表时。
Sql:
SELECT Project1.Id AS Id
, Project1.Name AS Name
, Project1.GeographyCategoryId AS GeographyCategoryId
, Project1.C1 AS C1
, Project1.Id1 AS Id1
, Project1.Name1 AS Name1
, Project1.GeographyCategoryId1 AS GeographyCategoryId1
FROM ( SELECT Extent1.Id AS Id
, Extent1.Name AS Name
, Extent1.GeographyCategoryId AS GeographyCategoryId
, Join1.Id AS Id1
, Join1.Name AS Name1
, Join1.GeographyCategoryId AS GeographyCategoryId1
, CASE WHEN ( Join1.SubRegion_Id IS NULL ) THEN CAST(NULL AS INT)
ELSE 1
END AS C1
FROM dbo.Regions AS Extent1
LEFT OUTER JOIN ( SELECT Extent2.SubRegion_Id AS SubRegion_Id
, Extent2.Region_Id AS Region_Id
, Extent3.Id AS Id
, Extent3.Name AS Name
, Extent3.GeographyCategoryId AS GeographyCategoryId
FROM dbo.SubRegionRegions AS Extent2
INNER JOIN dbo.SubRegions AS Extent3
ON Extent3.Id = Extent2.SubRegion_Id ) AS Join1
ON Extent1.Id = Join1.Region_Id ) AS Project1
ORDER BY Project1.Id ASC
, Project1.C1 ASC;
流畅的配置:
modelBuilder
.Entity<Region>()
.HasMany(t => t.SubRegions)
.WithMany(t => t.Regions)
.Map(m =>
{
m.ToTable("RegionSubRegion", "dbo");
m.MapLeftKey("RegionId");
m.MapRightKey("SubRegionId");
});
实体:
[Table("SubRegions", Schema = "dbo")]
public class SubRegion
{
public SubRegion()
{
}
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column(@"Id", Order = 1, TypeName = "int")]
[Required]
[Key]
public int Id { get; set; }
[Column(@"Name", Order = 2, TypeName = "varchar")]
[Required]
[MaxLength(50)]
[StringLength(50)]
public string Name { get; set; }
[Column(@"GeographyCategoryId", Order = 3, TypeName = "int")]
[Required]
public int GeographyCategoryId { get; set; }
[ForeignKey("GeographyCategoryId")]
public virtual GeographyCategory GeographyCategory { get; set; }
public virtual ICollection<Region> Regions { get; set; }
public virtual ICollection<Territory> Territories { get; set; }
public virtual ICollection<Employee> EmployeesResponsible { get; set; }
}
[Table("Regions", Schema = "dbo")]
public class Region
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column(@"Id", Order = 1, TypeName = "int")]
[Required]
[Key]
public int Id { get; set; }
[Column(@"Name", Order = 2, TypeName = "varchar")]
[Required]
[MaxLength(50)]
[StringLength(50)]
public string Name { get; set; }
[Column(@"GeographyCategoryId", Order = 3, TypeName = "int")]
[Required]
public int GeographyCategoryId { get; set; }
[ForeignKey("GeographyCategoryId")]
public virtual GeographyCategory GeographyCategory { get; set; }
public virtual ICollection<SubRegion> SubRegions { get; set; }
public virtual ICollection<Employee> EmployeesResponsible { get; set; }
}
表定义:
-- SubRegions
CREATE TABLE dbo.SubRegions
(
Id INT NOT NULL IDENTITY(1, 1) NOT FOR REPLICATION
, Name VARCHAR(50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
, GeographyCategoryId INT NOT NULL
)
ON [PRIMARY];
GO
ALTER TABLE dbo.SubRegions ADD CONSTRAINT PK_SubRegions PRIMARY KEY CLUSTERED (Id) WITH (FILLFACTOR=80) ON [PRIMARY];
GO
CREATE NONCLUSTERED INDEX IX_FK_GeographyCategorySubRegion ON dbo.SubRegions (GeographyCategoryId) WITH (FILLFACTOR=80) ON [PRIMARY];
GO
ALTER TABLE dbo.SubRegions ADD CONSTRAINT FK_SubRegions_GeographyCategories FOREIGN KEY (GeographyCategoryId) REFERENCES dbo.GeographyCategories (Id);
GO
-- Regions
CREATE TABLE dbo.Regions
(
Id INT NOT NULL IDENTITY(1, 1) NOT FOR REPLICATION
, Name VARCHAR(50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
, GeographyCategoryId INT NOT NULL
)
ON [PRIMARY];
GO
ALTER TABLE dbo.Regions ADD CONSTRAINT PK_Regions PRIMARY KEY CLUSTERED (Id) WITH (FILLFACTOR=80) ON [PRIMARY];
GO
CREATE NONCLUSTERED INDEX IX_FK_GeographyCategoryRegion ON dbo.Regions (GeographyCategoryId) WITH (FILLFACTOR=80) ON [PRIMARY];
GO
ALTER TABLE dbo.Regions ADD CONSTRAINT FK_Regions_GeographyCategories FOREIGN KEY (GeographyCategoryId) REFERENCES dbo.GeographyCategories (Id);
GO
-- RegionSubRegions
CREATE TABLE dbo.RegionSubRegion
(
RegionId INT NOT NULL
, SubRegionId INT NOT NULL
)
ON [PRIMARY];
GO
ALTER TABLE dbo.RegionSubRegion ADD CONSTRAINT PK_SubRegionRegion PRIMARY KEY NONCLUSTERED (SubRegionId, RegionId) WITH (FILLFACTOR=80) ON [PRIMARY];
GO
CREATE NONCLUSTERED INDEX IX_FK_SubRegionRegion_Region ON dbo.RegionSubRegion (RegionId) WITH (FILLFACTOR=80) ON [PRIMARY];
GO
ALTER TABLE dbo.RegionSubRegion ADD CONSTRAINT FK_SubRegionRegion_Region FOREIGN KEY (RegionId) REFERENCES dbo.Regions (Id);
GO
ALTER TABLE dbo.RegionSubRegion ADD CONSTRAINT FK_SubRegionRegion_SubRegion FOREIGN KEY (SubRegionId) REFERENCES dbo.SubRegions (Id);
GO
它是根据惯例生成自己的。为什么不在模型和配置中使用指定的列和表名?我错过了什么?
【问题讨论】:
标签: c# entity-framework