Quantcast
Channel: Entity Framework
Viewing all articles
Browse latest Browse all 10318

Edited Unassigned: EF5 - Migration Code isn't creating working code [1448]

$
0
0
I've got the following class definitions:

```
public partial class User
{
public int UserId { get; set; }
public int BlastersMembersID { get; set; }
public string Name { get; set; }

[DoNotSerialize]
public virtual blastersmember BlastersMember { get; set; }

/// <summary>
/// A list of game sessions this user has participated in recently
/// </summary>
public virtual ICollection<GameSessionEntry> GameSessionEntries { get; set; }

/// <summary>
/// A secure token is a token a user sends down to an app server upon joining a game so that the app server
/// can verify the user incoming is actually who they say they are. This also helps identify different users on a network.
/// Security tokens are unique so they are represented as a GUID which is long and guarenteed to be unique.
/// </summary>
[NotMapped]
public Guid SecureToken { get; set; }

/// <summary>
/// A session config is a set of properties used for a specific session
/// </summary>
[NotMapped]
public UserSessionConfig SessionConfig { get; set; }

/// <summary>
/// The current session this user is in, null if the user is not in a session.
/// </summary>
[NotMapped]
public GameSession.GameSession CurrentSession { get; set; }

/// <summary>
/// The date in which this user was registered and created for the game
/// </summary>
public DateTime CreationDate { get; set; }

/// <summary>
/// The connection associated with this given user
/// </summary>
[DoNotSerialize]
[NotMapped]
public NetConnection Connection { get; set; }

public User(NetConnection connection, string username)
{
Name = username;
Connection = connection;
}

public User()
{

}

public override string ToString()
{
return Name;
}


}
```

```
/// <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.
```
```

Viewing all articles
Browse latest Browse all 10318

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>