1

I'm trying to do some work with the EFProviderWrapperToolkit http://blogs.msdn.com/b/jkowalski/archive/2009/06/11/tracing-and-caching-in-entity-framework-available-on-msdn-code-gallery.aspx

In my derived DbCommandWrapper, is there any way to get the associated entity in the ObjectStateManager if the DbCommandTree in question is a DbModificationCommandTree?

Basically, I want to do something like:

if (base.Definition.CommandTree is DbModificationCommandTree)
{
   var targetEntity = ((DbModificationCommandTree)base.Definition.CommandTree).TargetEntity; 
}

3 Answers 3

1

Function CreateDbCommandDefinition of DbProviderServices has two parameters: DbProviderManifest manifest, DbCommandTree commandTree.

commandTree can be DbInsertCommandTree, DbUpdateCommandTree or DbDeleteCommasndTree.

So you can detect is it modification command or not.

Sign up to request clarification or add additional context in comments.

Comments

0

No. I don't think it is possible - it is layered architecture and low level layer don't have to know anything about upper layer.

1 Comment

I figured that might be the case....is there at least a way I can get the order that the statements will be generated in with relation to the ObjectStateEntries?
0

It seems you might be able to get some useful information from the Target property of System.Data.Common.CommandTrees.DbModificationCommandTree.

In particular, for an update statement you may see the following structure associated with the Target property if you drill down a bit:

Target (DbExpressionBinding)
 - Expression (DbScanExpression)
    - Target (System.Data.Metadata.Edm.EntitySetBase)
       - Name (string)

The Name refers to the name of the entity set in your entity container.

Similarly, you could examine the Predicate property of DbUpdateCommandTree or DbDeleteCommandTree to determine the key of the affected entity. For an entity with an integer column Id for a primary key, this structure could look like:

Predicate (DbComparisonExpression)
 - Left (DbPropertyExpression)
    - Property (EdmMember)
       - Name: "Id"
 - Right (DbConstantExpression)
    - Value: 1

Keep in mind that the value might be a parameter reference instead of a constant and that entities with composite keys would have more complex predicates.

Once you've extracted the entity set name and key information, you can construct an EntityKey and get the state by calling ObjectStateManager.GetObjectStateEntry(key).

Obviously this approach relies on knowledge of how entity framework constructs its command trees and doesn't extend well beyond simple statements. Using DbExpressionVisitor to focus on particularly important structural components of the tree may help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.