C# EF多对多配置
My DbContext似乎忽略了特定实体的fluent配置。它在C# EF多对多配置,c#,entity-framework,C#,Entity Framework,My DbContext似乎忽略了特定实体的fluent配置。它在外部连接的子查询中生成表subcionregions,而我在fluent配置中明确指定了表 Sql: 流畅的配置: modelBuilder .Entity<Region>() .HasMany(t => t.SubRegions) .WithMany(t => t.Regions) .Map(m => { m.ToTable("
外部连接
的子查询中生成表subcionregions
,而我在fluent配置中明确指定了表
Sql:
流畅的配置:
modelBuilder
.Entity<Region>()
.HasMany(t => t.SubRegions)
.WithMany(t => t.Regions)
.Map(m =>
{
m.ToTable("RegionSubRegion", "dbo");
m.MapLeftKey("RegionId");
m.MapRightKey("SubRegionId");
});
它是根据惯例自己产生的。为什么不在模型和配置中使用指定的列和表名?我遗漏了什么?在尝试调试和逐步完成单元测试之后,我发现我的单元测试是针对错误/过时的引用程序集构建的 更新引用解决了该问题
这么多小时。在尝试调试和逐步完成单元测试之后,我发现我的单元测试是针对错误/过时的引用程序集构建的 更新引用解决了该问题 这么多小时
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