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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:01:13+00:00 2026-05-29T10:01:13+00:00

I re-posted this question as I think it is a bit vague. New Post

  • 0

I re-posted this question as I think it is a bit vague. New Post

I am currently using a Windows Service that is on a 2 minute timer. I am using EF code first with a repository pattern for data access. I am using Ninject to inject my dependencies. I have the following bindings in my NinjectDependencyResolver class:

ConnectionStringSettings connectionStringSettings = ConfigurationManager.ConnectionStrings["Database"];

Bind<IDatabaseFactory>().To<DatabaseFactory>()
                        .InSingletonScope()
                        .WithConstructorArgument("connectionString", connectionStringSettings.Name);

Bind<IUnitOfWork>().To<UnitOfWork>().InSingletonScope();
Bind<IMyRepository>().To<MyRepository>().InSingletonScope();

When my service runs every 2 minutes I do some thing similar to this:

foreach (var row in rows)
{
    var existing = myRepository.GetById(row.Id);

    if (existing == null)
    {
        existing = new Row();

        myRepository.Add(existing);

        unitOfWork.Commit();
    }
}

I am starting to see an error in my logs that say:

The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: AcceptChanges cannot continue because the object’s key values conflict with another object in the ObjectStateManager. Make sure that the key values are unique before calling AcceptChanges.

Is it correct to use InSingeltonScope when using Ninject in a Windows Service? I believe I tried using different scopes like InTransientScope but I could only get InSingeltonScope to work with data access. Does the error message have anything to do with Scope or is it unrelated?

  • 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-05-29T10:01:16+00:00Added an answer on May 29, 2026 at 10:01 am

    Assuming that the service is not the only process that operates on the database you shouldn’t use Singleton. What happens in this case is that you are reusing a DBContext that has cached entities which are out of date.

    The better way is to treat each timer execution of the service in a similar way like it is a web/wcf request and create a new job processor for the request.

    var processor = factory.CreateRowsProcessor();
    processor.ProcessRows(rows);
    
    public class RowsProcessor
    {
        public Processor(UoW uow, ....)
        {
            ...
        }
    
        public void ProcessRows(Rows[] rows)
        {
            foreach (var row in rows)
            {
                var existing = myRepository.GetById(row.Id);
    
                if (existing == null)
                {
                    existing = new Row();
                    myRepository.Add(existing);
    
                    unitOfWork.Commit();
                }
            }
        }
    }
    

    Depending of the problem it might even better to put the loop outside and have a new processor for each single row.

    Read http://www.planetgeek.ch/2011/12/31/ninject-extensions-factory-introduction/ for more information about factories. Also have a look at the InCallScope of the named scope extension if you need to inject the UoW into multiple classes. http://www.planetgeek.ch/2010/12/08/how-to-use-the-additional-ninject-scopes-of-namedscope/

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

Sidebar

Related Questions

I posted this question on superuser a bit ago, but I haven't gotten an
Yesterday I posted this question regarding using lambdas inside of a Join() method to
Update I don't think I was clear enough when I originally posted this quesion.
I posted this question yesterday evening, which has led me to discover a huge
I posted this question: https://stackoverflow.com/questions/418597/java-and-net-for-php-programmer and the answers I was given didn't really help
I posted this question on Reddit Programming and did not get a single response.
I posted this question here on purpose, although - for my case - it
I posted this question at ActiveState but got no reply from support or in
I posted this question on the IntelliJ community forums but it seems as if
I just posted this question and learned about <see cref=> , however when i

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.