My problem is actually quite straight forward:
This is the MySQL table "ClubCategory". As you can see it links a club to a category.
+------------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+------+------+-----+---------+-------+
| CategoryId | int | NO | PRI | NULL | |
| ClubId | int | NO | PRI | NULL | |
+------------+------+------+-----+---------+-------+
The problem is as follows: My backing C# class has to implement an Interface that specifies an additional property named OtherId where OtherId is just an alias for CategoryId.
The class looks as follows
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;
}
}
I basically need to be able to use either ClubCategory.CategoryId or ClubCategory.OtherId to access the same database column CategoryId.
The Fluent API mapping I tried looks like this:
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);
However the resulting generated MySQL query when accessing an instance of this class
SELECT `c`.`ClubId`, `c`.`CategoryId`, `c`.`OtherId`, `c`.`CategoryId`
FROM `club2category` AS `c`
is obviously completely broken. Not only does it specify CategoryId twice but it also tries to access a fictional column named OtherId that doesn't exist in the database :|
So what do I need to change in Fluent API to successfully map both properties to the same MySQL column? Or is it possible at all? Any help would be hugely appreciated :)