views:

834

answers:

1

Hi, 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

+2  A: 

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

Robert Harvey