2 つのかなり単純なクラス (マップとゲーム) があり、このクエリを実行すると:
if(gameCollection.Any(g => g.NumPlayers <GameConstants.MAX_PLAYERS_PER_GAME))
エラーが表示されます: クラス MOMGame.GameObjects.Map をクラス MOMGame.GameObjects.Game に割り当てることはできません。既知の型がマップされたクラスから派生していることを確認してください。パラメータ名: タイプ
問題は何ですか?Mongo 2.2 に対して最新バージョンのドライバー (1.6.0.4624) を使用しています。
問題の 2 つのクラスは次のとおりです。
[BsonKnownTypes(typeof(MOMGame.GameObjects.Map), typeof(MOMGame.GameObjects.Location), typeof(MOMGame.GameObjects.Game), typeof(MOMGame.GameObjects.UserInfo))]
public class Game
{
[BsonId]
public Guid Id { get; set; }
public Map Map { get; set; }
public string Name { get; set; }
public List<Guid> Players { get; set; }
[BsonRepresentation(BsonType.Int32)]
public int NumPlayers { get; set; }
public Game()
{
Players = new List<Guid>();
//if (!BsonClassMap.IsClassMapRegistered(typeof(Game)))
//{
// BsonClassMap.RegisterClassMap<Game>(cm => cm.AutoMap());
//}
}
public static void SaveGame(Game game)
{
var connectionString = "mongodb://localhost/?safe=true";
var server = MongoServer.Create(connectionString);
var database = server.GetDatabase("mom");
var gameCollection = database.GetCollection<Game>("games");
game.NumPlayers = game.Players.Count;
gameCollection.Save(game);
}
public static Game GetNextGame()
{
var connectionString = "mongodb://localhost/?safe=true";
var server = MongoServer.Create(connectionString);
var database = server.GetDatabase("mom");
var gameCollection = database.GetCollection<Game>("games").AsQueryable();
if(gameCollection.Any(g => g.NumPlayers < GameConstants.MAX_PLAYERS_PER_GAME))
{
return gameCollection.First(g => g.NumPlayers < GameConstants.MAX_PLAYERS_PER_GAME);
}
else
{
return GameCreator.CreateGame();
}
}
}
[BsonKnownTypes(typeof(MOMGame.GameObjects.Map), typeof(MOMGame.GameObjects.Location) , typeof(MOMGame.GameObjects.Game), typeof(MOMGame.GameObjects.UserInfo))]
public class Map
{
public Map()
{
//if (!BsonClassMap.IsClassMapRegistered(typeof(Map)))
//{
// BsonClassMap.RegisterClassMap<Map>(cm => cm.AutoMap());
//}
}
[BsonIgnore]
public ILocation[][] Grid { get; set; }
[BsonId]
public Guid Id { get; set; }
public string Name { get; set; }
[BsonIgnore]
public List<ILocation> Locations
{
get
{
List<ILocation> locs = new List<ILocation>();
for (int i = 0; i < Grid.Length; i++)
{
for (int j = 0; j < Grid[i].Length; j++)
{
locs.Add(Grid[i][j]);
}
}
return locs;
}
}
}