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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:43:49+00:00 2026-05-18T19:43:49+00:00

I’m mapping a set of membership classes for my application using Fluent NHibernate. I’m

  • 0

I’m mapping a set of membership classes for my application using Fluent NHibernate. I’m mapping the classes to the asp.net membership database structure. The database schema relevant to the problem looks like this:

ASPNET_USERS
UserId        PK
ApplicationId FK NOT NULL
other user columns ...

ASPNET_MEMBERSHIP
UserId        PK,FK
ApplicationID FK NOT NULL
other membership columns...

There is a one to one relationship between these two tables. I’m attempting to join the two tables together and map data from both tables to a single ‘User’ entity which looks like this:

public class User
{
    public virtual Guid Id { get; set; }
    public virtual Guid ApplicationId { get; set; }

    // other properties to be mapped from aspnetuser/membership tables ...

My mapping file is as follows:

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Table("aspnet_Users");
        Id(user => user.Id).Column("UserId").GeneratedBy.GuidComb();
        Map(user => user.ApplicationId);
        // other user mappings

        Join("aspnet_Membership", join => {
            join.KeyColumn("UserId");
            join.Map(user => user.ApplicationId);
            // Map other things from membership to 'User' class
        }
    }
}

If I try to run with the code above I get a FluentConfiguration exception

Tried to add property ‘ApplicationId’ when already added.

If I remove the line “Map(user => user.ApplicationId);” or change it to “Map(user => user.ApplicationId).Not.Update().Not.Insert();“ then the application runs but I get the following exception when trying to insert a new user:

Cannot insert the value NULL into column ‘ApplicationId’, table ‘ASPNETUsers_Dev.dbo.aspnet_Users’; column does not allow nulls. INSERT fails.
The statement has been terminated.

And if I leave the .Map(user => user.ApplicationId) as it originally was and make either of those changes to the join.Map(user => user.ApplicationId) then I get the same exception above except of course the exception is related to an insert into the aspnet_Membership table

So… how do I do this kind of mapping assuming I can’t change my database schema?

  • 1 1 Answer
  • 1 View
  • 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-18T19:43:50+00:00Added an answer on May 18, 2026 at 7:43 pm

    I think I got something that works.

      public class Application
        {
            public virtual Guid ApplicationId { get; set; }
    
    
            /* Scalar Properties of an Application  */
            public virtual string ApplicationName { get; set; }
            public virtual string Description { get; set; }
    
            public virtual string LoweredApplicationName 
            {
                get
                {
                    return this.ApplicationName.ToLower();
                }
                set
                {
                    if (String.IsNullOrEmpty(this.ApplicationName))
                    {
                        this.ApplicationName = value;
                    }
                } 
            }
    
            public virtual IList<Membership> TheManyMemberships { get; protected set; }
    
        }
    
    
        public class User
        {
            public virtual Guid Id { get; set; }
            public virtual Application TheApplication { get; set; }
    
            public virtual Membership TheMembership { get; set; }
    
            /* Scalar Properties of a User  */
            public virtual string UserName { get; set; }
        }
    
    
        public class Membership
        {
            private Guid UserId { get; set; }
            private User _theUser { get; set; }
    
            protected Membership() { }
    
            public Membership(User theUser)
            {
                _theUser = theUser;
            }
    
            public virtual Application TheApplication { get; set; }
    
            /* Scalar Properties of a Membership  */
            public virtual string Password { get; set; }
    }
    
    
    
    
    
        public class ApplicationMap : ClassMap<Application>
        {
            public ApplicationMap()
            {
                Table("aspnet_Applications");
                Id(app => app.ApplicationId).Column("ApplicationId").GeneratedBy.GuidComb();
                Map(x => x.ApplicationName );
                Map(x => x.LoweredApplicationName);
                Map(x => x.Description );
    
                HasMany<Membership>(x => x.TheManyMemberships)
                    .Inverse()
                    .AsBag();
            }
        }
    
    
        public class UserMap : ClassMap<User>
        {
            public UserMap()
            {
                Table("aspnet_Users");
                Id(user => user.Id).Column("UserId").GeneratedBy.GuidComb();
                References(x => x.TheApplication, "ApplicationId")
                      .Not.Nullable();
    
                HasOne(x => x.TheMembership)
                .Cascade.All();//
                //.Constrained();
    
                Map(x => x.UserName).Not.Nullable();
    
            }
        }
    
    
       public class MembershipMap : ClassMap<Membership>
        {
            public MembershipMap()
            {
                Table("aspnet_Membership");
    
                Id(Reveal.Member<Membership>("UserId"))
                    .GeneratedBy.Foreign("_theUser");
                HasOne(
                  Reveal.Member<Membership, User>("_theUser"))
                        .Constrained()
                        .ForeignKey();
    
                References<Application>(x => x.TheApplication, "ApplicationId")
                .Not.Nullable();
    
                Map(x => x.Password);
    
            }
        }
    

    Forgive some of the naming conventions, when prototyping, I use un-ambiguous names over proper-convention to avoid confusion.

    The DDL I have (from the above code) and the DDL from the output of the asp.net (4.0) (using aspnet_regsql.exe to build the DDL) seem consistent (between the two versions).

    I need to thank this post:
    http://brunoreis.com/tech/fluent-nhibernate-hasone-how-implement-one-to-one-relationship/

    If you make any tweaks, then please post them.

    But I was able to save an Application, User and Membership.

    However, I think I may be slightly off with the User:Membership relationship.
    The microsoft scenario seems to be “Have a user, but allow that user to have a different password for each application”, which makes sense.
    But sometimes when using the MembershipProvider code (the MS code, nothing to do with NHibernate, I “feel” like sometimes it assumes a single application.

    I feel like the MS DDL should have a unique constraint on
    dbo.Membership (UserId, ApplicationId), but I don’t see it in their DDL.

    Regardless, this should provide some food for thought.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have thousands of HTML files to process using Groovy/Java and I need to

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.