Getting Started With EntityClient in EntityFramework


In this post I am going to illustrate how to use EdmGen and EntityClient to program with Entity Framework.

EdmGen is utility shipped with Entity Framework for creation of the artifacts required for Entity Model Mapping viz the CSDL(Conceptual Schema Definition Language), SSDL(Storage Model) and MSL(Mapping Model). Though you can use the EDM wizard in Visual Studio 2008 sp1, but since you may find a lot of references how to use the EDM wizard on internet and really less information on usage of Edmgen, I resorted to this.

Lets see how to create the artifacts for our School Database Model. The School database can be generated from the scripts available at MSDN here. [Creating the School Sample Database http://msdn.microsoft.com/en-us/library/bb399731.aspx]

Open up Visual Studio 2008 Command Prompt and then fire the following command

c:\> edmgen /connectionstring:"server=.\yukon;integrated security=true;database=school" /mode:FullGeneration /project:"EntityClientDemo"

Here is the explanation of  the switches used with Edmgen.exe

  • /ConnectionString : specifies the connection string used for connecting to the database
  • /FullGeneration : Generate ssdl, msl, csdl, and objects from the database
  • /Project : The base name to use for the artifact files.

NOTE: You need to specify either this switch or all the /OutXXXXX switches if you are using the /FullGeneration switch. To get more details on the various switches use EdmGen -help

Next, go ahead and create a new project in Visual Studio 2008. For this post, I created a console application. And add reference System.Data.Entity.

Here is the code

 

using (EntityConnection conn = new EntityConnection())
           {
               try 
               {
                  conn.ConnectionString =
@"Metadata=C:\Users\BindeshV\Documents\Visual Studio 2008\Projects\EntityFramework\EFClientDemo\EFClientDemo\Artifacts;" + @"Provider=System.Data.SqlClient;"+ @"Provider Connection String='Server=.\yukon;Integrated Security=yes;Database=School'"; conn.Open(); EntityCommand cmd = conn.CreateCommand(); cmd.CommandText = "Select C.CourseID,C.Title from EFClientDemoContext.Course as C"; cmd.CommandType = CommandType.Text; EntityDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess); while(reader.Read()) { Console.WriteLine("Course Id={0} Course Title={1}",reader.GetInt32(0),reader.GetString(1)); } } catch(Exception ex) { Console.WriteLine(ex.StackTrace); } } Console.Read();

Usage of EntityClient is somewhat similar to that of the SqlClient.

The ConnectionString property of the EntityConnection object has 3 keywords

  • Metadata - The path where are the artifacts reside. In this case the .csdl,.ssdl and msl.
  • Provider - The ADO.NET provider used to connect to the Database Store
  • Provider Connection String - The regular connection string

Notice that the CommandText uses the format of ContainerName.EntityName . In our case the EdmGen tool had the following information for Container Name

<Schema Namespace="EFClientDemo" Alias="Self" xmlns="http://schemas.microsoft.com/ado/2006/04/edm">
  <EntityContainer Name="EFClientDemoContext">

Here is the output of the program

BlogEFClientDemoOutput

Thanks

Skip to main content