C# Driver: "Ensure that known types are derived from the mapped class." error

Showing 1-2 of 2 messages
C# Driver: "Ensure that known types are derived from the mapped class." error Dan Terrill 10/6/12 6:46 AM
Folks,

I just started working on a game as a hobby project, and when I execute this query:

 if(gameCollection.Any(g => g.NumPlayers < GameConstants.MAX_PLAYERS_PER_GAME))
            {
                return gameCollection.First(g => g.NumPlayers < GameConstants.MAX_PLAYERS_PER_GAME);
            }

I get the error "

Class MOMGame.GameObjects.Map cannot be assigned to Class MOMGame.GameObjects.Game.  Ensure that known types are derived from the mapped class.
Parameter name: type
"


Any idea what I'm doing wrong? The 2 classes are pretty simple, and listed below.


Game:

[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();
            }
        }
    }


Map:

[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;

            }
        }
    }







Re: [mongodb-user] C# Driver: "Ensure that known types are derived from the mapped class." error Robert Stam 10/6/12 9:21 AM
[BsonKnownTypes] is used to list known *subclasses* of a root class.

Try just removing the [BsonKnownTypes] and see how that works.

If there is still an issue after that, please provide a stack trace also.

Thanks.








--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb