Chris Umbel

Using Entity Framework with Oracle

My post is relevant to VS 2008. Although I haven't tested it this article on Model-First entities with Oracle seems to cover the connectivity with 2010. Thanks to Selim Selcuk for tracking this down!

I've long thought that the Entity Framework hasn't gotten a fair shake. Sure, there's some room for improvement but for an out-of-the-box ORM that ships with the framework it's not too bad.

Oracle LogoOne thing has always troubled me, though. I was never sure what kind of traction it had with data sources other than SQL Server. Recently in a project circumstances conspired in such a fashion that it would be pretty darn handy to use EF to access data in Oracle. Turns out it that despite being a little rough around the edges it's very feasible.

The Provider

The first step is to download and install an Entity Framework provider for Oracle. There are several options out there but the Oracle Data Provider for Entity Framework on CodePlex seems to be the most complete that was free.

The Database

To demonstrate I'll set up a simple oracle express database to store hockey player data.

create table  "TEAMS" 
   (	"ID" number, 
	"NAME" varchar2(256), 
	"CITY" varchar2(256), 
	 constraint "TEAMS_PK" primary key ("ID") enable
   )
/

create trigger  "BI_TEAMS" 
  before insert on "TEAMS"               
  for each row  
begin   
    select "TEAMS_SEQ".nextval into :NEW.ID from dual; 
end; 

/
alter trigger  "BI_TEAMS" enable
/

create table  "PLAYERS" 
   (	"ID" number, 
	"FULLNAME" varchar2(256), 
	"JERSEYNUMBER" number, 
	"TEAM_ID" number, 
	 constraint "PLAYERS_PK" primary key ("ID") enable, 
	 constraint "PLAYERS_FK" foreign key ("TEAM_ID")
	  references  "TEAMS" ("ID") enable
   )
/

create trigger  "BI_PLAYERS" 
  before insert on "PLAYERS"               
  for each row  
begin   
    select "PLAYERS_SEQ".nextval into :NEW.ID from dual; 
end; 

/
alter trigger  "BI_PLAYERS" enable
/

The Plumbing

With the schema and prerequisite out of the way I can get right into adding an entity model to the project in the standard way. Add entity model to project dialog

I'll generate the model from an existing database. Choose model contents dialog

Now to set up the data source. Because of the Oracle Provider that I installed above there's a new "Oracle Database" option in the Data Source list. Data source setup dialog

Now I'll set up the connection information. Connection properties dialog

Next I'll select the tables I created above to create the model from. Choose database objects dialog

Ok, now I have a basic model, but I might as well clean up the naming by using pascal-case and reasonable pluralization. Entity model designer

If this were SQL Server I'd be done. Unfortunately there's a hidden problem. The ID fields that are generated from sequences won't be handled correctly. After saving the entities the ID's will be returned as 0. I'll fix this by manually hacking the SSDL (open your .edmx file in a text editor) with StoreGeneratedPattern="Identity" attributes on the ID fields (lines 6 and 16). Note that designer may rip that change out upon future modification.

While I suppose it's not absolutely necessary it might also be prudent to modify some type metadata such as changing "number"s to "int"s in your SSDL and "Decimal"s to "Int32"s in your CSDL where applicable. Frequently these don't auto-generate with the desired values especially with XE.


  
    
  
  
  
  
  


  
    
  
  
  
  

The Code

The plumbing work is over! Now I'll use the following C# to populate some data into my Oracle database.

using (HockeyEntities entites = new HockeyEntities())
{
    Team team = new Team();
    team.Name = "Penguins";
    team.City = "Pittsburgh";
    entites.AddToTeams(team);

    Player player = new Player();
    player.FullName = "Evgeni Malkin";
    player.JerseyNumber = 71;
    player.Team = team;

    player = new Player();
    player.FullName = "Sidney Crosby";
    player.JerseyNumber = 87;
    player.Team = team;

    team = new Team();
    team.Name = "Capitals";
    team.City = "Washington";
    entites.AddToTeams(team);

    player = new Player();
    player.FullName = "Alexander Ovechkin";
    player.JerseyNumber = 8;
    player.Team = team;

    entites.SaveChanges();
}

And I can query the data created above with:

using (HockeyEntities entites = new HockeyEntities())
{
    var players = from p in entites.Players
            where p.Team.City == "Pittsburgh"
            select p;

    foreach (Player p in players)
    {
        Console.WriteLine(p.FullName);
    }
}

Resulting in:

Evgeni Malkin
Sidney Crosby

Conclusion

Other that some minor tweaking to SSDL using EF to access Oracle isn't too bad. I assume that as time goes on issues like that will get worked out and the experience will be improve.

Tue Dec 22 2009 21:30:00 GMT+0000 (UTC)

Comments

Solutions to Common VirtualBox Problems

My favorite free, desktop-class virtualization system by far is Sun's VirtualBox. It reminds me a good bit of VMWare Workstation but, well... Free!

It's not perfect, however. How could you expect it to be? The number of combinations of physical hardware, virtual hardware, and guest/host operating systems are grand indeed.

I'd like to share a few solutions to common problems I came across lately while working with it. From what I can tell these problems are widespread and cause many people headaches. In the same spirit of my previous tools post I'll keep adding to it as time goes by.

Problem: Linux installs freeze when booting at:
NET: Registered protocol family 2
Host: At least Windows 7, 64-bit
Guest: Many linux distros.
Solution: Add the following kernel parameters:
noapic nolapic noacpi

Problem: Even with Guest Additions I can't get a resolution better than 1024x768 .
Host: Any
Guest: Various Linux's
Solution: Add a "Modes" line to the Screen/Display section of your /etc/X11/xorg.cfg as follows:

Section "Screen"
	Identifier "Screen0"
	Device     "Videocard0"
	DefaultDepth     24
	SubSection "Display"
		Viewport   0 0
		Depth     24
		Modes "1440x900" "1280x800" "1024x768" "800x600"
	EndSubSection
EndSection

Problem: Guest operating system freezes upon any significant network traffic.
Host: At least Windows 7, 64-bit
Guest: Many linux distros.
Solution: Use one of the Intel virtual network cards rather than the PCnet Fast III default.

Sun Dec 20 2009 21:30:00 GMT+0000 (UTC)

Comments

Parallel Programming with the Task Parallel Library and PLINQ in .Net 4.0

.Net 4.0 Logo It's no secret that parallel computing is becoming more important. As clock speeds have stagnated and the number of cores per die have increased one thing has become clear. Software developers have to adapt to the current state of processors by writing code that's more parallelizable.

In the past many programmers have avoided parallel processing when possible mainly due to its complexity even in the face of an obvious increase in throughput. Those that have parallelized have often done it poorly and suffered through some serious misery as a result.

We're running out of options, though. In order to get more done faster we have to do more at once.

Development platform providers have been scrambling lately to try to simplify parallel development and minimize the amount of work we'll have to invest in writing parallel code. It's clear that there's much market share to be gained by handling this problem well.

Microsoft's Approach

In .Net 4.0 Microsoft's attacking the problem by way of the Task Parallel Library and Parallel LINQ. These were previously available as a separate package under the name Parallel Extensions but have now officially joined the framework.

Task Parallel Library

The Task Parallel Library, or TPL, provides a set of tools that simplify common chores associated with parallel programming. Not only does it ease initial development but it also simplifies maintenance by auto-scaling to the capabilities of the machine hosting the code.

Tasks

The core component of the TPL is the Task, which is a single operation that can be executed asynchronously. These tasks are enqueued in a thread pool which manages their execution. The plumbing of maintaining the queue is entirely automated making the job of the application programmer quite simple.

Consider the following example which executes two crude tasks simultaneously.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/* import the task parallel libary */
using System.Threading.Tasks;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            /* create a task that prints a bunch of "A"s */
            Task t1 = new Task(() => {
                    for(int i = 0; i <= 255; i++) {
                        Console.WriteLine("A");
                    }
                }
            );

            /* create a task that prints a bunch of "B"s */
            Task t2 = new Task(() => {
                    for (int i = 0; i <= 255; i++)
                    {
                        Console.WriteLine("B");
                    }
                }
            );

            /* execute each task */
            t1.Start();
            t2.Start();

            /* wait for both tasks to complete */
            t1.Wait();
            t2.Wait();
        }
    }
}

Here's a sample of possible output:

A
A
A
A
A
A
B
B
B
B
A
A
A
A
B
B
B

In that example two tasks run more or less concurrently. The real advantage of the thread pool in the back end is that I could queue up more tasks than my processors could handle and the pool would manage how they're worked through.

Simpler still you can execute tasks with the Invoke static method of the Parallel class. Note that the Invoke method blocks until all operations passed-in have completed.

 Parallel.Invoke(
    () => { /* operation 1 */
        for(int i = 0; i <= 255; i++) {
            Console.WriteLine("A");
        }
    },
    () => /* operation 2 */
    {
        for (int i = 0; i <= 255; i++)
        {
            Console.WriteLine("B");
        }
    }
 );

The Parallel class boasts a few other goodies as we'll see below.

Parallel Looping

With the basics out of the way lets look at a real time-saver. It's common to simply repeat an operation a number of times or execute on operation on each item in a list. The TPL provides a means of accomplishing that in parallel with the For and ForEach static methods of the Parallel class.

Here's a Parallel.For example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Parallel.For(0, 24, i => {
                        Console.WriteLine(i);
                    }
                );
        }
    }
}

Which could produce output similar to:

0
1
2
12
13
3
4
5
6
8
9
10
11
15
16
17
14
18
19
20
21
22
23
7

Parallel.ForEach is similar except it iterates an IEnumerable. For demonstrative purposes I'll create the functional equivalent of the example above except it will iterate a list of integers.

Parallel.ForEach<int>(Enumerable.Range(0, 32), n =>
{
    Console.WriteLine(n);
    }
);

Parallel LINQ

Another useful parallel processing tool available in .Net 4.0 is Parallel LINQ, or PLINQ. There really isn't much to it. Consider the following example which looks like your typical LINQ operation except the AsParallel method of IEnumerable is called resulting in an ParallelEnumerable. This allows LINQ operations to be performed in a parallel fashion.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {
        struct Item {
            public int Id;
            public int Value;
        }

        static void Main(string[] args)
        {
            List<Item> items = new List<Item>();

            Random rand = new Random();

            foreach (int i in Enumerable.Range(0, 256)) 
            {
                items.Add(new Item() { Id = i, Value = rand.Next(10) });
            }

            int ct = (from item in items.AsParallel()
                          where item.Value > 5
                          select item).Count();

            Console.WriteLine(ct);
        }
    }
}

An interesting and simple twist on the above sample that would look familiar to T-SQL developers is that it's easy to specify the maximum number of cores to be used with ParallelEnumerable's WithDegreeOfParallelism method.

int ct = (from item in items.AsParallel().WithDegreeOfParallelism(2)
              where item.Value > 5
              select item).Count();

Conclusion

Based on the examples above it's clear that parallel programming has become at least a bit simpler with .Net 4.0. I've only scratched the surface here. There are still many more parallel tools (some good, some bad) available in .Net 4.0 and I recommend checking out the Parallel Programming documentation at MSDN for a more complete story.

Mon Dec 14 2009 22:12:00 GMT+0000 (UTC)

Comments

Clojure, A Lisp for the JVM and CLR

I've been becoming increasingly interested in functional languages in the last few years and I'm apparently not the only one. It's pretty hard to listen to any general purpose software development podcast without hearing about Erlang, Haskell or F#. Another one came up recently that I just had to play with. It's a Lisp variant named Clojure.

Clojure Logo The reason I find Clojure particularly interesting is that it's designed to be hosted in the Java Virtual Machine and the .Net Common Language Runtime (via the DLR). From a practical perspective that's wonderful considering integration with other commonly used libraries in the business world is a snap. I'm sure it annoys Lisp purists, but it makes Clojure much more adoptable between 9 and 5.

As is frequently the case this is not something I'm an expert in and am by no means qualified to write a proper tutorial. I would, however, like to share some of the code I whipped up while aquainting myself with Clojure in the hopes it can help other Clojure neophytes. I'll cover the basic language, JVM/CLR integration and Software Transactional Memory (STM).

Factorials, the Classic Example

To demonstrate the basics for someone unfamilier with Lisp I'm going to take an approach that I haven't taken in my previous demonstrations. Here I'm going to solve the same problem a few different ways. It's a common, simple and functional problem: factorials. Consider all the following functionally equivalent function declarations:

Factorial Style 1: Recursion

(defn fact[n]
    (if (= n 1) 1
    (* n (fact (
    dec n)))))

There's a simple, more imperitive looking recursive implementation. By no means idiomatic in Lisp terms, but mathematically correct.

Factorial Style 2: Tail Recursion

(defn fact [n]
    ;; this is where tail recursion enters
    (loop [cnt n acc 1]
	;; we're done
        (if (= 1 cnt) acc
            ;; otherwise recurse
           (recur (dec cnt) (* acc cnt)))))

Example 2 uses the recur-loop form which, while recursive, won't consume additional stack space as style 1 would. Hence the benefit of tail recursion optimization.

Factorial Style 3: Reduction

(defn fact [n]
    (reduce * (range 1 (inc n))))

The approach in example 3, while iterative, is perhaps the most interesting. It uses the reduce and range functions that would be familiar to python programmers. reduce essentially starts out by applying a specified function, multiplication in this case, to two members in a set. The result of the function and the next member in the set are then fed back into the reduction function until the entire set has been processed. The range function is used here to produce a sequence upon which reduce will iterate.

Java Runtime

Now that we've covered some basics let's look at some interaction with the Java runtime. The following is my classic example of twitter status retrivial via the REST API:

(import '(javax.xml.parsers DocumentBuilderFactory DocumentBuilder)
        '(javax.xml.xpath XPathFactory XPath XPathExpression XPathConstants))

;; function to retrieve a twitter user's status
(defn getStatus[userName]
	(let [domFactory (. DocumentBuilderFactory newInstance)
		builder (. domFactory newDocumentBuilder)
		;; build the url 
		url (str "http://twitter.com/users/" userName ".xml")
		;; load contents located at the url into the builder
		doc (. builder parse url)
		;; create xpath plumbing
		factory (. XPathFactory newInstance)
		xpath (. factory newXPath)
		expr (. xpath compile "/user/status/text/text()")]
		;; pull the user's status out of the document
		(. expr evaluate doc (. XPathConstants STRING))))

(println (getStatus "chrisumbel"))

CLR

Here we go again with a twitter status example, this time in the .Net runtime.

(System.Reflection.Assembly/Load "System.Xml,
	Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
(import '(System.Xml XmlDocument))

;; function to retrieve a twitter user's status
(defn getStatus[userName] 
	(let [
		;; build the url 
		url (str "http://twitter.com/users/" userName ".xml")
		;; instanciate an XML document and load the contents located
		;; at the url into it
		doc (doto (XmlDocument.)
		(. Load url))]
		;; pull the user's status out of the document
		(. (. doc SelectSingleNode "/user/status/text") InnerText)))

(println (getStatus "chrisumbel"))

Speaking of XML

The above examples do a fine job of illustrating how to dig into the class libraries of the respective runtimes but they're anything but idiomatic because Clojure has it's own functional XML parsing system. Check out this post about xml-seq for details.

Software Transactional Memory

It's hard to discuss a language these days without evaluating how it handles concurrency. Clojure does not disappoint in this regard as it brings Software Transactional Memory (STM) to the table. Note the dosync call on line 11. Anything that happens within that scope is effectively wrapped in a transaction and is atomic. If a conflict occurs the transaction will be retried.

(import '(java.util.concurrent Executors))

(let [ val (ref 0)
    ;; thread pool will contain 4 threads
    pool (Executors/newFixedThreadPool 4)
    ;;  create a list of 4 tasks that count to 1000
    tasks (map (fn [t]
        (fn []
          (dotimes [n 1000]
            ;; start a transaction
            (dosync
              (doseq []
                ;;  add one to the val
                (alter val + 1)
                (println (str "OP1: " t " : " n " : " (deref val)))
                ;;  add another one to the val
                (alter val + 1)
                (println (str "OP2: " t " : " n " : " (deref val))))))))
        (range 4))]
    ;; spawn the threads
    (doseq [future (.invokeAll pool tasks)]
        (.get future))
        (.shutdown pool)))

Conclusion

Clojure is an interesting combination of old and new. Lisp was designed in 1958 and is the second oldest high level language. The virtual machines that Clojure runs on are far newer and concepts like STM are only now making their way into the mainstream. Somehow all of these technologies still appear to fit together at least well enough to the job done. I look forward to attempting a serious project in Clojure and definately recommend giving it a look.

Relevant Links

Sun Dec 13 2009 21:12:00 GMT+0000 (UTC)

Comments
< 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 >
Follow Chris
RSS Feed
Twitter
Facebook
CodePlex
github
LinkedIn
Google