Monday, 5 November 2012

Entity Framework System.Data.Objects?

Yes , Entity Framework contains all the objects System.Data.Objects namespace.


using (NorthwindEntities context = new NorthwindEntities())
{

  MyPerson emp = context.MyPerson.FirstOrDefault(emps => emps.ssname == "name3");

}
from Designer


   public global::System.Data.Objects.ObjectQuery<MyPerson> MyPerson
        {
            get
            {
                if ((this._MyPerson == null))
                {
                    this._MyPerson = base.CreateQuery<MyPerson>("[MyPerson]");
                }
                return this._MyPerson;
            }
        }


like
at code-behind


the same thing in code

ObjectQuery<MyPerson> cs = context.CreateQuery<MyPerson>("[MyPerson]");
MyPerson myperson = new MyPerson();
myperson=cs.FirstOrDefault(ems => ems.ssname == "name3");

another Demo:

ObjectContext
Provides facilities for querying and working with entity data as objects.

public ObjectQuery<T> CreateQuery<T>(string queryString, params ObjectParameter[] parameters);

ObjectQuery

Represents a typed query against an Entity Data Model (EDM) in a given object
Context.
EX :
"SELECT VALUE emp FROM NorthwindEntities.Employees AS emp";(typed query against Entity Data Model---Object Context(EntityFrameworkDemo.NorthwindEntities)
query.Context

{EntityFrameworkDemo.NorthwindEntities}
    [EntityFrameworkDemo.NorthwindEntities]: {EntityFrameworkDemo.NorthwindEntities}
    CommandTimeout: null
    Connection: {System.Data.EntityClient.EntityConnection}
    DefaultContainerName: "NorthwindEntities"
    MetadataWorkspace: {System.Data.Metadata.Edm.MetadataWorkspace}
    ObjectStateManager: {System.Data.Objects.ObjectStateManager}
  int iers  = entitycontext.CreateQuery<Employees>(sql).Count<Employees>();

this is Explored as
NorthwindEntities entitycontext = new NorthwindEntities();

int iers  = entitycontext.CreateQuery<Employees>(sql).Count<Employees>();
or
ObjectContext Contextt = new ObjectContext(entitycontext.Connection.ConnectionString);
Contextt.Connection.Open();

var sql = "SELECT VALUE emp FROM NorthwindEntities.Employees AS emp";// [Entity-SQL]
                  // not select * from Employee-->error.
{

ObjectQuery<Employees> query = Contextt.CreateQuery<Employees>(sql);
int iers = query.Count<Employees>();
or
    int iers = Contextt.CreateQuery<Employees>(sql).Count<Employees>();


}

foreach (Employees emp in query)
Console.WriteLine("{0} {1} {2} {3}", emp.EmployeeID, emp.FirstName, emp.LastName, emp.Country);

Console.WriteLine("");

Console.Write("Cont :   " + iers);
Console.Read();
}

DEMO:
var sql = "SELECT VALUE emp FROM NorthwindEntities.Employees AS emp";
  ObjectQuery<Employees> iers;
  iers = Contextt.CreateQuery<Employees>(sql);
  iers.Name = "kasi";

============================
  ObjectQuery<Employees> i = iers.Where("kasi.Country = @country", new       ObjectParameter("country", "USA"));
Console.write(i.count<Employee>());
Or
==========================================================================
ObjectQuery<Employees> iers;  
iers = Contextt.CreateQuery<Employees>(sql);
iers.Name = "abc";
            
ObjectQuery<Employees> t= iers.Where("kasi.Country = @country", new ObjectParameter("country", "USA"));
t.Name = "def"// Container name.
foreach (Employees emp in t)
  Console.WriteLine("{0} {1} {2} {3}", emp.EmployeeID, emp.FirstName, emp.LastName, emp.Country);
               
  Console.WriteLine("");          
  Console.Write("Cont :   " + t.Count<Employees>());
  Console.Read();


ObjectContext Executes  impliectly for every database query:
Executes the given function on the default container.
Name of function. The name may include the container name,
such as <ContainerName>.<Function Name>.
 When the default container name is known, only the
        //     function name is required.
        //
        //   parameters:
        //     An array of System.Data.Objects.ObjectParameter objects.
        //
protected ObjectResult<TElement> ExecuteFunction<TElement>(string functionName, params ObjectParameter[] parameters) where TElement




No comments:

Post a Comment