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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:51:21+00:00 2026-06-03T02:51:21+00:00

I’m trying to use entity framework together with the default membership aspnetdb database. I’m

  • 0

I’m trying to use entity framework together with the default membership “aspnetdb” database.

I’m having trouble with membership and user tables, since I’d like to have a navigational property from membership to user.

I feel like I’ve tried every combination of fluent api mapping, but I must be missing something – or maybe it just can’t be done, when the foreign key is UserId – which is the primary key of both tables as well?

Here’s what I have:

public class Membership {
        [Key]
        public Guid UserID { get; set; }
        public String Password { get; set; }

        public virtual User User { get; set; }
    }

public class User {
        [Key]
        public Guid UserId { get; set; }
        public String UserName { get; set; }

        //public virtual Membership Membership { get; set; }
        public virtual ICollection<Role> Roles { get; set; }
    }



protected override void OnModelCreating(DbModelBuilder modelBuilder) {
            modelBuilder.Entity<Membership>().ToTable("dbo.aspnet_Membership");
            modelBuilder.Entity<Role>().ToTable("dbo.aspnet_Roles");
            modelBuilder.Entity<User>().ToTable("dbo.aspnet_Users");
            modelBuilder.Entity<User>()
                            .HasMany(m => m.Roles)
                            .WithMany(m => m.Users)
                            .Map(m => {
                                m.ToTable("aspnet_UsersInRoles");
                                m.MapLeftKey("UserId");
                                m.MapRightKey("RoleId");
                            });
            //modelBuilder.Entity<Membership>()
            //    .HasRequired(m => m.User);
                //.WithOptionalPrincipal(m => m.Membership).Map(m=>m.MapKey("UserId"));
            Database.SetInitializer<EkomiteDBC>(null);
            base.OnModelCreating(modelBuilder);
        }

The roles are set up fine, and works, but no matter how I try and define the connection between membership and user I keep getting different mapping errors.

I know I’m probably missing something basic, can anyone help?

EDIT: The problem is, it seems, that both “membership” and “users” tables have the primary key “UserID”.

  • 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-03T02:51:22+00:00Added an answer on June 3, 2026 at 2:51 am

    Try this:

    modelBuilder.Entity<Membership>()
                .HasRequired(m => m.User)
                .WithOptional(u => u.Membership);
    

    Anyway you should not do that at all. Use standard membership API for accessing these tables. These tables are not yours they belong to membership / role API and handling them through EF just removes or breaks functionality the API provides out of the box – in general it will reduce the security because those tables will be available for update in EF and business rules from Membership / Role API will not be enforced.

    • 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
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported
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

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.