How to code against EDM [Entiry Data Model]


This is an extension to one of the existing article,http://www.c-sharpcorner.com/UploadFile/jaishmathews/683/Default.aspx . Refer to the above mentioned article for a sample EDM. 
 
1. Entity Client API

We have bunch of API classes defined under "System.Data.EntityClient" namespace. We can use these classes along with EntirySQL to retrieve the information from an EDM. For information on EntitySQL, refer the article mentioned above. One of the mandatory class needs to be used is "EntityConnection" like below

EntityConnection conn = new EntityConnection(<<connectionString>>);

We also have EntityCommand and EntityDataReader, which can use like below

EntityCommand cmd = conn.CreateCommand();
string commandText = <<Any valid EntitySQL>>;
cmd.CommandText = commandText;
EntityDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess);

This "reader" object contains result of your EntitySQL. You can even bind this "reader" object to any DataBound control. EntityClient way of communicating with EDM has follwing features
  1. It's the fastest way to cummunicate with EDM
  2. it’s read-only 
  3. You can only retrieve back string-based data via the EntityDataReader.
  4. Your queries must be written using Entity SQL. 
2.Object Services

Object Services provides a nice layer on top of Entity Client and provides the object materialization that allows us to work with CLR objects.

When we generated our EDM, an object model was also generated for us that make working with our EDM using Object Services very easy. You can see it by opening the "Designer.cs" file of the EDM.

You can see that it created a class for every entity in our model as well as a context object that represents our Entity Container, like a database which contains alll tables.
As we make changes to our model through the designer, our generated classes will be updated to reflect any changes.

When working with Object Services, our connection to the EDM occurs through an ObjectContext class, which in turns wraps an EntityConnection. So no manual connection needed. We just need to create the object of the context 1st. 
 
Then access each entity through properties, like below.

INVENTORYENTITIES context = new INVENTORYENTITIES();
                var prods = context.PRODUCTS;
                foreach (var prod in prods)
                {
                } 

You can see we are not using any EntitySQL here. 

Good thing is that, you can use LINQ

var prods = from p in context.PRODUCTS where p.ProductName.Length > 30 select p;

You can manipulate data!!!

var product = (from p in context.PRODUCTS
                               where p.ProductID == 2
                               select p).First();
                product.ProductName = "DOVE";
                context.SaveChanges(); 

You can delete too

var product = (from p in context.PRODUCTS
                               where p.ProductID == 2
                               select p).First();
                context.DeleteObject(product); context.SaveChanges();
               
Selecting the appropriate way for coding EDM is in developers hand.
Ebook Download
View all
Learn
View all