3

My Domain Classes is as follow:

public class Author
{
    public int Id { get; set; }
    public string Name { get; set; }
    public IList<Post> Posts { get; set; }
}

public class Blog
{
    public int Id { get; set; }
    public string Name { get; set; }
    public IList<Post> Posts { get; set; }
}

public class Post
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
    public Author Author { get; set; }
    public Blog Blog { get; set; }        
}

As you see I have absolutely not any data annotation or attributes for entity framework annotations, and I Configure Entity framework related annotations in another classes for each one using entity framework fluent api. now I want to replace entity framework with MangoDb.

but in mongo db I needed to place an attribute at list for Id like below:

public class Author
{
    [BsonElement("_id")]
    [BsonRepresentation(BsonType.ObjectId)]
    public int Id { get; set; }
    public string Name { get; set; }
    public IList<Post> Posts { get; set; }
}

My Question is is there any way to this configuration outside in another class, and don't touch my poco classes like what we use in entity framework's fluent api.

mesut
  • 2,099
  • 3
  • 23
  • 35
  • 1
    Good question. I always find it annoying if I have to mix my implementation specific attributs in my code. – Carra Jul 31 '15 at 13:15

2 Answers2

4

You can do that by using a BsonClassMap class like:

BsonClassMap.RegisterClassMap<Post>(cm => 
{ 
    cm.MapMember(x => x.Title).SetElementName("_title");
});

The documentatins is here: Mapping Classes
Also there are default conventions, and for Id field you don't need to map it to _id name, it will be handled automatically.

Tim
  • 5,435
  • 7
  • 42
  • 62
FireAlkazar
  • 1,795
  • 1
  • 14
  • 27
  • Do i need so set the result of RegisterClassMap in IMongoCollection or in other place for that class? – Vinicius Gualberto Oct 28 '15 at 16:19
  • @ViniciusGualberto not sure if I understood you correctly. If you want to apply this mapping, you just write the mapping block of code anywhere you want. Typically it is done at application startup. – FireAlkazar Oct 28 '15 at 16:40
0

Basically you can use both EntityFramework and MongoDB driver attributes together. But I would not recommend you to use absolutely same data structure that you used in EntityFramework as SQL and NoSQL are completely different approaches. With NoSQL you should think more of how your application is going to consume your data and create your domain, chose optimal embedding strategy and apply indexes accordingly.

You can find some good reading on MongoDB website. Here are some links to start:

http://docs.mongodb.org/manual/core/data-model-design/

http://docs.mongodb.org/manual/core/data-modeling-introduction/

Andrei
  • 42,814
  • 35
  • 154
  • 218
  • thank you for your recommendation, but basically the behaviour of data is same, I have a blog, blog has one author and posts...... my question is to find a way to set mongodb's related attributes outside of the class. like what we use for entityframework onmodelcreating using fluentApi – mesut Jul 31 '15 at 13:30