我的问题实际上很简单:
这是 MySQL 表"ClubCategory"。如您所见,它将俱乐部与类别联系起来。
+------------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+------+------+-----+---------+-------+
| CategoryId | int | NO | PRI | NULL | |
| ClubId | int | NO | PRI | NULL | |
+------------+------+------+-----+---------+-------+
问题如下:我的支持 C# 类必须实现一个接口,该接口指定一个名为OtherId
where的附加属性,OtherId
它只是CategoryId
.
该类如下所示
public class ClubCategory : IClubFilterLinker
{
private int _categoryId;
public int ClubId { get; set; }
public int CategoryId
{
get => _categoryId;
set => _categoryId = value;
}
public int OtherId
{
get => _categoryId;
set => _categoryId = value;
}
}
我基本上需要能够使用ClubCategory.CategoryId
或ClubCategory.OtherId
访问相同的数据库列CategoryId
。
我尝试的 Fluent API 映射如下所示:
modelBuilder.Entity<ClubCategory>()
.Property(nameof(_categoryId))
.HasColumnName("CategoryId")
.HasColumnType("INT")
.IsRequired();
modelBuilder.Entity<ClubCategory>()
.Property(cc => cc.CategoryId)
.HasField(nameof(_categoryId))
.UsePropertyAccessMode(PropertyAccessMode.Field);
modelBuilder.Entity<ClubCategory>()
.Property(cc => cc.OtherId)
.HasField(nameof(_categoryId))
.UsePropertyAccessMode(PropertyAccessMode.Field);
但是,在访问此类的实例时生成的 MySQL 查询
SELECT `c`.`ClubId`, `c`.`CategoryId`, `c`.`OtherId`, `c`.`CategoryId`
FROM `club2category` AS `c`
显然是彻底坏掉了。它不仅指定CategoryId
了两次,而且还尝试访问OtherId
数据库中不存在的虚构列:|
那么我需要在 Fluent API 中进行哪些更改才能成功地将两个属性映射到同一个 MySQL 列?或者有可能吗?任何帮助将不胜感激:)