I've got the following class definitions:
```
public partial class User
{
public int UserId { get; set; }
```
```
/// <summary>
/// This is a <see cref="BlastersShared.GameSession"/> that is being entered into the database
/// </summary>
public class GameSessionEntry
{
/// <summary>
/// Constructor
/// </summary>
/// <param name="gameSession">The game session to create this log entry from</param>
public GameSessionEntry(GameSession.GameSession gameSession)
{
MatchName = gameSession.Name;
}
/// <summary>
/// The name of this match
/// </summary>
public string MatchName { get; set; }
/// <summary>
/// The unique index of this GameSession entry
/// </summary>
public long SessionId { get; set; }
/// <summary>
/// A list of users that were part of this match
/// </summary>
public virtual ICollection<User> Users { get; set; }
/// <summary>
/// The time in which this game offically started (that is, when someoen began playing)
/// </summary>
public DateTime StartDate { get; set; }
/// <summary>
/// This is the time when the game had offically ended
/// </summary>
public DateTime EndDate { get; set; }
/// <summary>
/// The type of game that was played
/// </summary>
public GameSessionType GameType { get; set; }
}
The Migration framework produces something like:
```
public override void Up()
{
RenameColumn(table: "user", name: "id", newName: "UserId");
CreateTable(
"GameSessionEntryUsers",
c => new
{
SessionId = c.Long(nullable: false),
UserId = c.Int(nullable: false),
})
.PrimaryKey(t => new { t.SessionId, t.UserId })
.ForeignKey("gamesession_logs", t => t.SessionId, cascadeDelete: true)
.ForeignKey("user", t => t.UserId, cascadeDelete: true)
.Index(t => t.SessionId)
.Index(t => t.UserId);
CreateTable(
"gamesession_logs",
c => new
{
SessionId = c.Long(nullable: false, identity: true),
MatchName = c.String(nullable: false, maxLength: 45, unicode: false, storeType: "nvarchar"),
StartDate = c.DateTime(nullable: false),
EndDate = c.DateTime(nullable: false),
GameType = c.Byte(nullable: false),
})
.PrimaryKey(t => t.SessionId);
}
```
This ends up with:
> The Foreign Key on table 'GameSessionEntryUsers' with columns 'UserId' could not be created because the principal key columns could not be determined. Use the AddForeignKey fluent API to fully specify the Foreign Key.
Many, many guides on the internet suggest this should work. I even tried mapping explicit such as:
```
modelBuilder.Entity<GameSessionEntry>().
HasMany(c => c.Users).
WithMany(p => p.GameSessionEntries).
Map(
m =>
{
m.MapLeftKey("SessionId");
m.MapRightKey("UserId");
m.ToTable("UserSessions");
});
The only thing I can think of at this point is some sort of bug in the EF.
```
```
```
public partial class User
{
public int UserId { get; set; }
```
```
/// <summary>
/// This is a <see cref="BlastersShared.GameSession"/> that is being entered into the database
/// </summary>
public class GameSessionEntry
{
/// <summary>
/// Constructor
/// </summary>
/// <param name="gameSession">The game session to create this log entry from</param>
public GameSessionEntry(GameSession.GameSession gameSession)
{
MatchName = gameSession.Name;
}
/// <summary>
/// The name of this match
/// </summary>
public string MatchName { get; set; }
/// <summary>
/// The unique index of this GameSession entry
/// </summary>
public long SessionId { get; set; }
/// <summary>
/// A list of users that were part of this match
/// </summary>
public virtual ICollection<User> Users { get; set; }
/// <summary>
/// The time in which this game offically started (that is, when someoen began playing)
/// </summary>
public DateTime StartDate { get; set; }
/// <summary>
/// This is the time when the game had offically ended
/// </summary>
public DateTime EndDate { get; set; }
/// <summary>
/// The type of game that was played
/// </summary>
public GameSessionType GameType { get; set; }
}
The Migration framework produces something like:
```
public override void Up()
{
RenameColumn(table: "user", name: "id", newName: "UserId");
CreateTable(
"GameSessionEntryUsers",
c => new
{
SessionId = c.Long(nullable: false),
UserId = c.Int(nullable: false),
})
.PrimaryKey(t => new { t.SessionId, t.UserId })
.ForeignKey("gamesession_logs", t => t.SessionId, cascadeDelete: true)
.ForeignKey("user", t => t.UserId, cascadeDelete: true)
.Index(t => t.SessionId)
.Index(t => t.UserId);
CreateTable(
"gamesession_logs",
c => new
{
SessionId = c.Long(nullable: false, identity: true),
MatchName = c.String(nullable: false, maxLength: 45, unicode: false, storeType: "nvarchar"),
StartDate = c.DateTime(nullable: false),
EndDate = c.DateTime(nullable: false),
GameType = c.Byte(nullable: false),
})
.PrimaryKey(t => t.SessionId);
}
```
This ends up with:
> The Foreign Key on table 'GameSessionEntryUsers' with columns 'UserId' could not be created because the principal key columns could not be determined. Use the AddForeignKey fluent API to fully specify the Foreign Key.
Many, many guides on the internet suggest this should work. I even tried mapping explicit such as:
```
modelBuilder.Entity<GameSessionEntry>().
HasMany(c => c.Users).
WithMany(p => p.GameSessionEntries).
Map(
m =>
{
m.MapLeftKey("SessionId");
m.MapRightKey("UserId");
m.ToTable("UserSessions");
});
The only thing I can think of at this point is some sort of bug in the EF.
```
```