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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:11:17+00:00 2026-06-08T14:11:17+00:00

I do not find correct way in my MVC app how to bind repositories

  • 0

I do not find correct way in my MVC app how to bind repositories and all EF related data. The example below create new DbContext for every IRepository and this give me error

An entity object cannot be referenced by multiple instances of
IEntityChangeTracker

This error appears because my Entities in different context. For example the code(it is in Entity) will give error

var user = new User();
_userRepository.Insert(user) 

var order = new Order();
order.User = user;
_orderRepository.Insert(order) 
_unitOfWork.Commit();

If I change

kernel.Bind(typeof(DbContext)).ToMethod(context => new DbContext(connectionString));

to

kernel.Bind(typeof(DbContext)).ToMethod(context => new DbContext(connectionString)).InRequestScope();

I get error

The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

if i use ServiceRepository in separated thread.

Maybe someone know solution?

var connectionString = ConfigurationManager.ConnectionStrings["Entities"].ConnectionString;            
kernel.Bind(typeof(DbContext)).ToMethod(context => new DbContext(connectionString));            
kernel.Bind<IObjectSetFactory>().ToMethod(c => kernel.Get<DbContextAdapter>());
kernel.Bind<IObjectContext>().ToMethod(c => kernel.Get<DbContextAdapter>());  
kernel.Bind(typeof(IUnitOfWork)).To(typeof(UnitOfWork));
kernel.Bind(typeof(IRepository<>)).To(typeof(Repository<>));
kernel.Bind<IServiceRepository>().To<ServiceRepository>();


 public interface IServiceRepository
    {
        UserDetail GetUser(int id);
        User GetUser(string email);
        User GetUser(string email, string password);
        OrderDetail GetOrder(string id);
        IEnumerable<OrderDetail> GetOrders(int userId);
        IEnumerable<Product> GetProducts();
        UserDetail GetParentUser(string partialEmail);
        IEnumerable<UserDetail> GetChildUsers(int parentId);
        IEnumerable<Statistic> GetStatisticForCurrentMonth(string ip);
        void InsertStatistic(QueueItem queueItem);
        void InsertStatistic();
        void Commit();
        void AddUser(User model);
        User AddUser(string firstName, string lastName, string email, string password, string country, int? parentId = null, DateTime? dateStamp = null);
        void AddOrder(Order order);
        void DeleteUser(int id);
        void DeleteUser(string email);
        bool OrderManager(PaymentProcessorOrder order, out User newUser, out Order newOrder);
        User AuthenticatedUser();
        string AuthenticatedUserEmail();
        bool ValidateUser(string email, string password);
        string GetPassword(string email);
    }


 public class ServiceRepository : IServiceRepository
    {
        private readonly IRepository<User> _userRepository;
        private readonly IRepository<Order> _orderRepository;
        private readonly IRepository<UserDetail> _userDetailRepository;
        private readonly IRepository<Statistic> _statisticRepository;
        private readonly IRepository<Product> _productRepository;
        private readonly IRepository<OrderDetail> _orderDetailRepository;
        private readonly IUnitOfWork _unitOfWork;
        private static readonly object Locker = new object();

  public ServiceRepository(IRepository<User> userRepository, IRepository<Statistic> statisticRepository, IRepository<UserDetail> userDetailRepository, IRepository<Order> orderRepository, IUnitOfWork unitOfWork, IRepository<OrderDetail> orderDetailRepository, IRepository<Product> productRepository)
        {
            _unitOfWork = unitOfWork;
            _userRepository = userRepository;
            _statisticRepository = statisticRepository;
            _userDetailRepository = userDetailRepository;
            _orderRepository = orderRepository;
            _orderDetailRepository = orderDetailRepository;
            _productRepository = productRepository;
        }

//Skip code
}
  • 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-08T14:11:19+00:00Added an answer on June 8, 2026 at 2:11 pm

    You shouldn’t use threads in an ASP.NET application because it gives you many problems. E.g. when IIS recycles you App Pool or suspends the application your threads will be terminated leaving your application in an inconsistent state. Or an uncatched exception in your thread can tear down your whole application.

    Usually, you should implement async things in a separate service application so that IIS just has to send some message which will be processed asyncronously be this service. This will also solve your problem as you can use an other DbContext for each message.

    If you really want to use background threads then you have to use different bindings for the DbContext for your requests and threads. E.g.

    kernel.Bind(typeof(DbContext)).ToMethod(context => new DbContext(connectionString)).InRequestScope();
    kernel.Bind(typeof(DbContext)).ToMethod(context => new DbContext(connectionString)).WhenInjectedInto<MyTask>();
    

    But this means you can’t pass entities from the request DbContext to the background thread context.

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

Sidebar

Related Questions

Currently learning Spring MVC with Spring 3, I'm trying to find the correct way
I'm not able to get this persistence file correct... I do not find any
I do not seem to find what version of a onkeyup is correct: camelcase:
I could not find a way to make a RTL paragraph in Docx using
What is the correct way to find the absolute path to the App_Data folder
started learning racket today. I was attempting to find the correct way to append
When trying to find solution for this below question: MVC Web Api Route with
I'm scratching my head to find the correct way to implement a dynamic sliding/expanding
I seriously cannot find the correct way to do this. I have this method
been waiting an hour to find out correct way to connect to mysql at

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.