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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T13:28:34+00:00 2026-06-18T13:28:34+00:00

I have a small webapp that uses EntityFramework to store stuff via repositories into

  • 0

I have a small webapp that uses EntityFramework to store stuff via repositories into the database.

What I’ve done so far (based on all the tutorials I read) is create a repository where I need it, as shown below:

In CustomMembershipProvider:

    public CustomMembershipProvider()
    {
        _userRepository = new UserRepository(new TenantApplicationContext());
    }

In my RegisterController:

    public TenantRepository TenantRepository { get; set; }
    public UserRepository UserRepository { get; set; }

    protected override void Initialize(RequestContext requestContext)
    {
        if (MembershipService == null) { MembershipService = new AccountMembershipService(); }
        if (TenantRepository == null) { TenantRepository = new TenantRepository(TenantApplicationContext); }
        if (UserRepository == null) { UserRepository = new UserRepository(TenantApplicationContext); }

        base.Initialize(requestContext);
    }

The point is, that I instantiate the UserRepository twice. This becomes a problem when I create a User in one instance, and try to retrieve it in the other instance, and I did not call SaveChanges in between.

The problem lies here:

// Snippet from the register controller class

if (!UserRepository.Exists(model.AccountableEmailAddress))
{
    // 1 - Create the user via a custom MembershipProvider
    // Note, the CustomMembershipProvider has it's own instance of UserRepository

    var createStatus = MembershipService.CreateUser(
        model.AccountableUser,
        model.Password,
        model.AccountableEmailAddress);

    if (createStatus == MembershipCreateStatus.Success)
    {
        // Left out irrelevant code

        AdministerUserAndTenant(tenant.Name, model.AccountableEmailAddress);
    }
}

private void AdministerUserAndTenant(string tenantName, string emailAddress)
{
    // 2 - Try to retrieve the user from a different (!) instance of UserRepository
    var user = UserRepository.GetUser(emailAddress);
    var tenant = TenantRepository.GetTenantByName(tenantName);
    tenant.Users.Add(user);
    TenantApplicationContext.SaveChanges();
}

I hope you can still follow, tried to leave out unnecessary parts.

What is the best way to deal with issues like this?

PS: I’m not very fond of the Singleton pattern, so if possible don’t go there :).

  • 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-18T13:28:35+00:00Added an answer on June 18, 2026 at 1:28 pm

    When exactly does it become a problem? Cause that’s where the answer lies. Classes that should know of each other’s unsaved changes should use the same repository instance. Since they are probably related, you’ll manage passing a reference between them.

    If there’s reason why all of your application should have one single repository, use Dependency Injection.

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

Sidebar

Related Questions

I am working toward a small webapp that I would like to have a
I have a small Spring MVC webapp (which embeds ActiveMQ) that is designed to
I have a small web app that uses a simple username/password login scheme. I've
i have written a small webapp using spring-security and spring-mvc with an annotation based
I'm wrinting a webapp in bottle. I have a small interface that lets user
I have written a small webapp using the flask framework that involves plotting using
I have created my first uber small webapp with MVC4. So far I used
I have small web app that generate PDF files as a report. I'm trying
I have small script in bash, which is generating graphs via gnuplot. Everything works
I am developing a small web app that uses jQuery quite a lot. In

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.