Entity Framework: Tips and Tricks, part 3

October 19th, 2010

This article continues series of publications about non-trivial Entity Framework situations encountered by our users.

We consider the following questions in this article:

1. Why Oracle raises an exception about too many local sessions participating in global transaction when using TransactionScope?


I have run the following test code:

and it failed after the 32 iterations. The error I get is “ORA-02045: too many local sessions participating in
global transaction”. One more strange thing is the fact that everything works like a charm in Direct mode.
Could you please explain the behaviour?


Maximum number of branches in the distributed Oracle transaciton is 32. We use distributed Oracle transactions in our code that implements TransactionScope support. So, as soon as the number of transactions inside the scope exceeds this number this error appears. The reason of the fact that Direct mode works is the fact that there is no distributed transactions support in it (local transactions are used). So, the workaround is to use TransactionScope with smaller number of transaction branches. In most scenarios it is enough to use a local transaction like in the following example:

Note: Starting from the last version of dotConnect for Oracle we solve this problem with the help of promotable single phase transaction support. Only one active session with internal local transaction is used during several connections opening. You can activate this option in connection string setting up the parameter “Transaction Scope Local=true”.

2. How can I optimize SQL queries using Entity Framework?


When I am using native SQL I can easily use hints for MySQL optimization. Can I use anything like this when using Entity Framework?


You have two alternatives – the first one is to create views in your database implementing the desired behaviour, and the second one is to manually create Defining Query in the Storage model and entities in your conceptual model corresponding to these Defining Queries.

In both cases you will be able to use the MySQL hints.

3. How can I perform cascade deleting of objects in Entity Framework?


Do I really have to delete all child entities separately? Is there any possibility to use cascaded deletes?


Add Cascade action for all associations you want to be cascaded. This action should be added both in CSDL and SSDL parts of the .edmx file. Here is an example of CSDL part:
Example of SSDL is shown below:
As you can see to add cascade deleting just define the following construction in CSDL or SSDL part of your .edmx file:

4. How can I get the default value of a database field in my Entity Framework project?


I have a varchar2 “MyProperty” column in my Oracle database containing the default value. How can I get this value, e.g, to populate my Textbox in the user input form?


You need to query MetadataWorkspace, for example, in the following way:

We hope this material will be useful for you. Please provide your feedbacks and suggestions in comments to article.

2 Responses to “Entity Framework: Tips and Tricks, part 3”

  1. Sebastian Says:

    If I add the setting “TransactionScopeLocal=true” to my connection string as suggested in the note at the end of question #1 I get an exception telling me that the connection string parameter is not supported. Is this parameter really implemented in the current version?

  2. Devart Says:

    Thank you for the comment. We have made a correction, the parameter name should be “Transaction Scope Local”. This functionality is available since the latest 6.0.46 Beta build of dotConnect for Oracle.