0

我有 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;

            }
        }
    }
4

1 回答 1

0

我认为您不需要 KnownTypes 属性,因此请尝试删除这些属性。它们是为了明确让 Mongo 知道任何子类,而您列出的那些似乎不是从属性所在的类派生的

于 2012-10-06T15:50:24.813 回答