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 520779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:10:51+00:00 2026-05-13T08:10:51+00:00

I have been trying for quite a while to work out how to accomplish

  • 0

I have been trying for quite a while to work out how to accomplish my mission, and in fact I am not even sure that the direction I am heading in, is indeed a diserable one in terms of my SQL tables – but it is the most concise I have been able to think up on.

My problem is fairly simple to describe. I have a table containing comments, with a foreign-key to my User table (which represents the author of the comment). In an attempt to properly support anonymous users I have decided to make this foreign key nullable – and I also provide 3 columns for specifying Name, Email and Website where I wish to store the information relevant in case the user is anonymous – these fields are also nullable.

So in short

  • If the user is logged in, the foreign-key will contain the ID of that user and Name, Email and Website will be null.
  • If the user is not logged in, the foreign-key will be null and Name, Email and Website will contain data about the user.

In my domain model I wish to map this to a User class (a property, Author, on my comment class). This User class then have a property called “IsAnonymous” – which should be set accordingly.

Ideally, I would have some mapping set up that would map this Author property depending on the values of the foreign-key and assign these properties correct. I already have a mapping for my User class that maps it to the User table, but I am not sure how I would do this the other way.

I have tried looking into the IUserType interface, but I can’t quite understand how I would ago about this issue of having to read from 2 different tables.

Is there a reasonable way of doing this using my current database structure – or should I consider switching my layout, and if so, to what?

I switched from having a simple column in my User table indicating an anonymous user, but this started giving me issues when the Name column is set as a unique key. I want registered Users to have unique Name’s but not anonymous Users.

Thanks for your time.

EDIT: Read over that and decided maybe some ASCII/pseudo-SQL diagrams would come in handy

User
-----
UserId Guid PRIMARY KEY
Name nvarchar(200) UNIQUE KEY NOT NULL
Email nvarchar(200) NOT NULL
Website nvarchar(200) NOT NULL

Comment
-------
CommentId Guid PRIMARY KEY
UserId GUID - FK-USER
Name nvarchar(200)
Email nvarchar(200)
Website nvarchar(200)
  • 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-05-13T08:10:52+00:00Added an answer on May 13, 2026 at 8:10 am

    I’d have something like that

    //map this in NH
    public virtual User LoggedInCreator {get;set;}
    //Not mapped
    public virtual User CreatorInformation {
        get {
            if(LoggedInCreator != null) return LoggedInCreator;
            return new User {
                    Name = AnonymousCommenterName,
                    Email = AnonymousCommenterEmail,
                    Website= AnonymousCommenterWebsite
                };
        }
    }
    public void SetAnonymouscommenter(string name, string email, string website)
    {
        LoggedInCreator = null;
        AnonymousCommenterName = name;
        AnonymousCommenterEmail = email;
        AnonymousCommenterWebsite = website;
    }
    

    However, if you want to do it in a much cleaner way, you’d use a great feature of NHibernate, which is being able to map Fields and also mapping properties that store their values in fields.
    Here is what a cleaner implementation would be:

    protected User _commenter;
    public virtual User Commenter
    {
        get {
            if(_commenter != null) return _commenter;
            return new User {
                    Name = AnonymousCommenterName,
                    Email = AnonymousCommenterEmail,
                    Website= AnonymousCommenterWebsite
            };
        }
        set {
            bool isAnonymous = value.Id == 0;
            _commenter = isAnonymous ? null : value;
            AnonymousCommenterName = isAnonymous ? value.Name : null;
            AnonymousCommenterEmail = isAnonymous ? value.Email : null;
            AnonymousCommenterWebsite = isAnonymous ? value.Website : null;
        }
    }
    

    You can see they are essentially the same idea though, just some cleanup using NHibernate features.

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

Sidebar

Related Questions

No related questions found

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.