Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7903449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:45:27+00:00 2026-06-03T09:45:27+00:00

I would like to have a reference between two entities stored in the RavenDB

  • 0

I would like to have a reference between two entities stored in the RavenDB document database. Since this is not a relational db I know that I am supposed to use the Denormalized Reference technique described on RavenDBs documentation. Whilst at first this seems fine, once I start to create a real-world domain ‘hierarchy’ including bidirectional references the effort of keeping all those references up to date feels disproportionate. I feel I may be going wrong somewhere.

Can you explain the best / simplest way to model a reasonably complex domain hierarchy using RavenDB?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T09:45:28+00:00Added an answer on June 3, 2026 at 9:45 am

    I am not sure whether this will go far enough to answer your question but here is how I go about creating a Denormalized Reference in RavenDB (this is taken from real code with non-essentials removed for clarity)

    Domain

    public class User : IUserIdentity
    {
        public string UserName { get; set; }
        public IEnumerable<string> Claims { get; set; }
        public string Id { get; set; }
        public Guid FormsAuthenticationGuid { get; set; }
    }
    
    public class Assessment
    { 
        public string Id { get; set; }
        public UserReference User { get; set; }
        public AssessmentState State { get; set; }
    }
    

    You can see that I have a Assessment class that references a User. This user reference are managed using the UserReference class below.

    Denormalized Reference

    public class UserReference
    {
        public string Id { get; set; }
        public string UserName { get; set; }
    
        public static implicit operator UserReference(User user)
        {
            return new UserReference
                    {
                            Id = user.Id,
                            UserName = user.UserName
                    };
        }
    }
    

    Note how the reference class also carries the UserName. This value will not change very often but it may change so we need a way to update the UserName property in the UserReference property held in the Assessment class. To make the change we must first find the correct Assessment instances from RavenDB and for that we need an index.

    Raven Index

    public class Assessment_ByUserId : AbstractIndexCreationTask<Assessment>
    {
        public Assessment_ByUserId()
        {
            Map = assessments => from assessment in assessments
                                    select new
                                        {
                                                User_Id = assessment.User.Id
                                        };
        }
    }
    

    This index needs to be invoked whenever a User‘s UserName value is updated. I have a UserService class that helps me co-ordinate all my User related functions, so that is where I put this code.

    I reuse this code for other references so it has been abstracted out a little. This may help you create the more complex hierarchies (or perhaps ‘domain graph’ is a better description) you want.

    UserService

    public static void SetUserName(IDocumentSession db, string userId, string userName)
    {
        var user = db.Load<User>(userId);
        user.UserName = userName;
        db.Save(user);
        UpdateDenormalizedReferences(db, user, userName);
    }
    
    private static void UpdateDenormalizedReferences(IDocumentSession db, User user, string userName)
    {
        db.Advanced.DatabaseCommands.UpdateByIndex(
                RavenIndexes.IndexAssessmentByUserId,
                GetQuery(user.Id),
                GetUserNamePatch(userName),
                allowStale: true);
    
    }
    
    private static IndexQuery GetQuery(string propertyValue, string propertyName = "User_Id")
    {
        return new IndexQuery {Query = string.Format("{0}:{1}", propertyName, propertyValue)};
    }
    
    private static PatchRequest[] GetUserNamePatch(string referenceValue, string referenceName = "User")
    {
        return new[]
                {
                        new PatchRequest
                        {
                                Type = PatchCommandType.Modify,
                                Name = referenceName,
                                Nested = new[]
                                        {
                                                new PatchRequest
                                                {
                                                        Type = PatchCommandType.Set,
                                                        Name = "UserName",
                                                        Value = referenceValue
                                                }
                                        }
                        }
                };
    }
    

    That is it. And you know, now that I lay it all out I can see what you mean. It is a lot of work just to update a reference. Perhaps the Service code can be made more DRY and reused for different relationship types, but I don’t see how to get away from writing lots of indexes, one per referenced type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to have an explicit cast between from a string to an
I would like to add a gap between two buttons on an Eclipse toolbar.
I would like to have a generic Interpolator class which can interpolate between instances
I have parent child relationship between two entities(Parent and Child). My Parent mapping is
I would like to have a checkbox preference that takes the user to a
So in my project i would like have a nice treeview that has images.
I would like to have a Windows 2003 server fire a script to fire
I would like to have a QMainWindow that can change it's look at runtime,
I would like to have Ajax form in Rails so i'm using form_remote_tag. The
I would like to have the keyboard already displaying when I show my view.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.