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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:27:21+00:00 2026-06-18T07:27:21+00:00

I have a class Landlord that inherits from UserProfile using table-per-type inheritance. When a

  • 0

I have a class Landlord that inherits from UserProfile using table-per-type inheritance.

When a new user registers on the application, they enter some criteria and select the type of account they want, either Landlord or Tenant.

Here’s my AccountController/Register method:

public ActionResult Register(RegisterModel model)
    {
        if (ModelState.IsValid)
        {
            // Attempt to register the user
            try
            {
                WebSecurity.CreateUserAndAccount(model.UserName, model.Password,
                                            new
                                            {
                                                Email = model.Email,
                                                FirstName = model.FirstName,
                                                LastName = model.LastName,
                                                AccountType = model.AccountType.ToString()
                                            },
                                            false);

                // Add user to role that corresponds with selected account type
                if (model.AccountType == AccountType.Tenant)
                {
                    try
                    {
                        Roles.AddUserToRole(model.UserName, "Tenant");

                        using (var db = new LetLordContext())
                        {
                            var tenant = db.UserProfile.Create<Tenant>();

                            tenant.TenantAge = null;
                            tenant.TenantGroupMembers = null;
                            tenant.UserId = WebSecurity.CurrentUserId;
                            tenant.UserName = model.UserName;
                            // more properties associated with tenant
                            // ...

                            db.UserProfile.Add(tenant);
                            db.SaveChanges();
                        }

                    }
                    catch (ArgumentException e)
                    {
                        ModelState.AddModelError("Unable to add user to role", e);
                    }
                }
                if (model.AccountType == AccountType.Landlord)
                {
                    try
                    {
                        Roles.AddUserToRole(model.UserName, "Landlord");

                        using (var db = new LetLordContext())
                        {
                            var landlord = db.UserProfile.Create<Landlord>();

                            // same idea as adding a tenant
                        }
                    }
                    catch (ArgumentException e)
                    {
                        ModelState.AddModelError("Unable to add user to role", e);
                    }
                }

                return RedirectToAction("Confirm", "Home");
            }
            catch (MembershipCreateUserException e)
            {
                ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

As an example, if I selected Tenant as the desired account type when registering, WebSecurity.CreateUserAndAccount would add a user into the UserProfile table, correctly, with say a UserProfileId of 1.

Then, if (model.AccountType == AccountType.Tenant) would see that the selected account type is Tenant, add the user to that role, with a UserProfileId of 1 and a RoleId of 1. Within this if-statement, because selected role is Tenant, I create a new Tenant like so: var tenant = db.UserProfile.Create<Tenant>(); and it is saved to the database (with correct UserProfileID as the PK).

The problem: Two UserProfile entities (two rows) are being added to the UserProfile table each time I try to register ONE user. I understand that this is probably due to the fact that I am calling WebSecurity.CreateUserAndAccount AND I’m creating a new Tenant object.

How do I avoid this situation?

How do I add the model being used in WebSecurity.CreateUserAndAccount into UserProfile table and Tenant table ONCE?

  • 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-18T07:27:22+00:00Added an answer on June 18, 2026 at 7:27 am

    In this instance you would not create both a UserProfile and a Tenant or Landlord. Once the Entity is created the entity type cannot be changed (even to extend it to a subtype). So in your case, you only need to skip the step of creating the UserProfile and just create and save either the Tenant or the Landlord entity that inherits it.

    More info linked from Option 1 of my answer to this question: Code First TPT Inheritance – How can I supply the foreign key instead of EF creating the parent row for me?

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

Sidebar

Related Questions

I have class that is derived from FrameworkElement , and I want WPF to
I have class A, and class B that inherits A in Eclipse workspace. The
I have class B derived from class A. I call copy constructor that I
I have class that has a property that is of type string[]. I need
I have class A derived from a Windows.Form class, using the VS2008 designer. When
I have class A that has some primitive attributes and also member of type
I have class LegacyClass which inherits OldBaseClass. I'm considering a change to introduce a
I have class that extend FragmentActivity in it I add fragment to layout as
I have class grades that I need to check if a specific grade is
I have class that looks like this: class A { public: class variables_map vm

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.