Simple Inheritance Mapping Question

84 views
Skip to first unread message

wpkramer

unread,
Jun 18, 2012, 4:11:39 PM6/18/12
to mongodb...@googlegroups.com
Hello,
 
I am quite new MongoDB.  I am trying not to decorate the domain classes with the MongoDb attributes, but am having difficulty mapping the data members of the parent class.  The statement "BsonMemberMap mapLastUpdated = cm.GetMemberMap(c => c.LastUpdated);" in the example below is assigning a null.
 
How can I get the BsonMemberMap for a parent class's data member?
 
 
 
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Options;
using MongoDB.Driver.Builders;
using MongoDB.Driver.GridFS;
using MongoDB.Driver.Linq;
namespace MongoHierarchy
{
    public abstract class DomainEntity
    {
        public DomainEntity()
        {
            LastUpdated = DateTime.MinValue;
        }
        public DateTime LastUpdated { get; set; }
    }
    public sealed class Widget : DomainEntity
    {
        public string Name { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            BsonClassMap.RegisterClassMap<Widget>(cm =>
            {
                cm.AutoMap();
                cm.SetIdMember(cm.GetMemberMap(c => c.Name));
                BsonMemberMap mapLastUpdated = cm.GetMemberMap(c => c.LastUpdated);
                mapLastUpdated.SetSerializationOptions(
                    new DateTimeSerializationOptions { Kind = DateTimeKind.Local });
            });
            string key = "Test1";
            Widget test = new Widget();
            test.Name = key;
            test.LastUpdated = DateTime.Now;
            var connectionString = "mongodb://localhost/?safe=true";
            var server = MongoServer.Create(connectionString);
            var database = server.GetDatabase("test");
            var widgets = database.GetCollection<Widget>("widgets");
            widgets.Insert(test);
            test = null;
            var query = Query.EQ("_id", key);
            test = widgets.FindOne(query);
            widgets.Remove(query);
        }
    }
}

craiggwilson

unread,
Jun 19, 2012, 10:05:45 AM6/19/12
to mongodb...@googlegroups.com
You need to use the parent's class map to do that.

BsonClassMap.RegisterClassMap<DomainEntity>(cm =>
{
  cm.AutoMap();
  var memberMap = cm.GetMemberMap(c => c.LastUpdated);
});

wpkramer

unread,
Jun 19, 2012, 1:39:08 PM6/19/12
to mongodb...@googlegroups.com
thank you
Reply all
Reply to author
Forward
0 new messages