Re: [mongodb-user] C# Driver: "Ensure that known types are derived from the mapped class." error

235 views
Skip to first unread message

Robert Stam

unread,
Oct 6, 2012, 12:21:17 PM10/6/12
to mongod...@googlegroups.com
[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.

On Sat, Oct 6, 2012 at 9:46 AM, Dan Terrill <dan.t...@c8sciences.com> wrote:
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;

            }
        }
    }







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

Reply all
Reply to author
Forward
0 new messages