In my data model I have a fairly common division between my objects/tables/data:
- "Transactional" entities that represent the work that is being done by the system. These entities are created by the system and are only important in specific contexts. They are regularly created on the fly. (Aside: Is there a proper name for this type of entity?)
- "Data Dictionary" entities that represent common properties of the transactional entities. These are defined irregularly (mostly at the start of the project) and have a much more static lifecycle. They are typically created by me.
So, for example, I might have a User (transactional) entity and a UserType (data dictionary) entity.
I want to (hard)code references to instances of the Data Dictionary entities into my code. This gives me the ability to code business logic with an easy-to-understand language. (So, for example, I might have a UserType of "Plain" and a UserType of "Admin", and then a rule that says "allow access only if the User's UserType equals Admin").
I'm using LINQ-to-Entities as my data access technology/ORM. To implement the Data Dictionary references, I'm storing EntityKeys. My understanding is that they're detached from the object context and so are suitable for this purpose. (As they don't contain entity state, I also don't have to worry about that state going stale.)
However, this is giving me problems when I try to add a new transactional entity with a DD-EntityKey-reference. Continuing my example, I'm doing this:
UserEntities userEntities = new UserEntitites()
User user = new User()
user.UserType = new UserType()
user.UserType.EntityKey = adminEntityKey
userEntities.AddToUser(user)
...and this gives me the following error:
System.InvalidOperationException : The object cannot be added to the ObjectStateManager because it already has an EntityKey. Use ObjectContext.Attach to attach an object that has an existing key.
If I try to call userEntities.Attach(user) instead of AddToUser, I get this:
System.InvalidOperationException : An object with a null EntityKey value cannot be attached to an object context.
Both of these errors make sense, given the mixing of new and preexisting entities that I'm doing. What I'm not sure about is how to get around this issue. Is there some way I can have detached references to DD-entities and assign them to new attached objects without requiring me to load the entire DD-entity state?