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

  • Home
  • SEARCH
  • 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 246907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:12:41+00:00 2026-05-11T21:12:41+00:00

I’ve got an application where I’m implementing a singleton pattern in the database, and

  • 0

I’ve got an application where I’m implementing a singleton pattern in the database, and I’m running into huge problems.

I’ve got the following Table, SingletonTable

+-------------+-------+------------+
| Column Name | Type  | Allow Null |
+-------------+-------+------------+
| Id          | Int   |  No        |
| VarType     | Int   |  No        |
| TypeARef    | Int   |  Yes       |
| TypeBRef    | Int   |  Yes       | 
+-------------+-------+------------+

In the EF, this is broken down into two concrete types, TypeA and TypeB. TypeARef is on TypeA, TypeBRef is on TypeB, and VarType is the discriminator. There is a unique index on (VariableType, TypeARef, TypeBRef). The idea of the singleton pattern is that we only ever have 1 row in the database for a given value tuple, so whenever we add a reference to to SingletonTable, we first check to see if that row exists, if it does, we return the row in the DB, if it doesn’t, we create the row and return a reference to the newly created row. I’ve abstracted this out into a method, GetSingleton.

The problem is whenever I use the method GetSingleton, I get the follwoing Exception:

Test method UnitTests.CreateBasicData.CreateFTIData threw exception: System.InvalidOperationException: The object could not be added to the EntityCollection or EntityReference. An object that is attached to an ObjectContext cannot be added to an EntityCollection or EntityReference that is not associated with a source object

I’m using it as such

var newRow = new SomeType
{
        singletonValue = GetSingleton(new TypeB{ TypeBRef = Foo })
}

The problem appears to be that I can’t add a reference from something not already in the data context to something that is in the data context. Is this right? How do I work around this limitation?

Thanks,
Roy

  • 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-11T21:12:42+00:00Added an answer on May 11, 2026 at 9:12 pm

    When a query is executed inside an object context in the Entity Framework, the returned objects are automatically attached to the object context.

    You can also attach objects to an object context that are obtained from a source other than a query. You might attach objects that have been previously detached, objects that have been returned by a NoTracking query, or objects that have been obtained outside the object context. You can also attach objects that have been stored in the view state of an ASP.NET application or have been returned from a remote method call or Web service.

    Use one of the following methods to attach the object to an object context:

    • Call AddObject on ObjectContext to add the object to the object
      context. Do this when the object is a new object that does not yet
      exist in the data source.

    • Call Attach on ObjectContext to attach the object to the object
      context. Do this when the object already exists in the data source
      but is currently not attached to the context. For more information,
      see How to: Attach Related Objects (Entity Framework).

    • Call AttachTo on ObjectContext to attach the object to a specific
      entity set in the object context. Also do this if the object has a
      null (Nothing in Visual Basic) EntityKey value.

    http://msdn.microsoft.com/en-us/library/bb896271.aspx

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

Sidebar

Ask A Question

Stats

  • Questions 142k
  • Answers 142k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer that's because you need a carriage return and line feed.… May 12, 2026 at 8:20 am
  • Editorial Team
    Editorial Team added an answer It seems to me that you are trying to pull… May 12, 2026 at 8:20 am
  • Editorial Team
    Editorial Team added an answer you can define css properites within the element to be… May 12, 2026 at 8:20 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.