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.

1
  • 1
    Good question. I always find it annoying if I have to mix my implementation specific attributs in my code. Commented Jul 31, 2015 at 13:15

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Do i need so set the result of RegisterClassMap in IMongoCollection or in other place for that class?
@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.
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/

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.