ObjectContext
is more imporved in from Version of Entity Framework 4.0.
In case of 3.5 version -à No LazyLoadingEnabled
support.
What is LazyloadingEnabled support?
In First Realise of EDM only current objects only
loaded and referenced objects are not loaded.
EX:
using (NorthwindEntities context = new NorthwindEntities())
{
ObjectQuery<Products> products = context.Products;
foreach
(Products item in
products)
{
Console.WriteLine(item.ProductName);
Console.WriteLine(item.Categories.CategoryName);--NullRefrenceException
Console.Write(item.Discontinued
+ "\n");
}
Console.Read();
}
LazyLoading Support: EF 4.0 onwords
ObjectContextOptions :
LazyloadingEnable( default is falue). But while
generating .edmx or EF generator automatically enables the Lazyloading is set
to-àtrue.
public NorthwindEntities() : base("name=NorthwindEntities", "NorthwindEntities")
{
this.ContextOptions.LazyLoadingEnabled = true;
OnContextCreated();
}
using (NorthwindEntities
context=new NorthwindEntities())
{
ObjectSet<Product>
products = context.Products;
foreach
(Product item in
products)
{
Console.WriteLine(item.ProductName);
Console.WriteLine(item.Category.CategoryName);--No Exception here
Console.Write(item.Discontinued +"\n");
}
Console.Read();
}
Note:
If we comment the this.ContextOptions.LazyLoadingEnabled
= true; // we can again NullRefrenceException.
ObjectQuery
Represents a query against the store.
This query is formulated through an
Entity SQL statement,
Query Builder methods, or Language-Integrated Query
(LINQ).
query
against the store-àInstead Developer
overhead of Entity-SQL. Prepared against the STORE
using (NorthwindEntities
context = new NorthwindEntities())
{
ObjectQuery<Products> Produt = context.Products;
foreach
(Products item in
Produt)
{
Console.WriteLine(item.ProductName);
//Console.WriteLine(item.Category.CategoryName);---not
supported 3.5
Console.Write(item.Discontinued
+ "\n");
}
Console.Read();
}
ObjectSet:
This is
also added in Entity Framework 4.0 onwords only. Which enable we can directly
execute native commands (not required any Entity-SQL). In their databases.
ObjectSet : Represents
a typed Entity set
to perform CRUD operations.
ObjectQuery<T>
Represents
a typed query
against a conceptual model in a given object context.
Then
ObjectSet<TEntity> : ObjectQuery<TEntity>
Demo:
using (NorthwindEntities
context=new NorthwindEntities())
{
ObjectSet<Product>
Produt=context.Products;
IQueryable<Product>
query =Produt.Where(s
=> s.CategoryID == 4);
foreach (Product item in query)
{
Console.WriteLine(item.ProductName);
}
Console.Read();
}
which done through
Immediate window : code and config
code:
query.Provider--àtakes apporiate Provider to execute the command.// Provider
can be SqlClient,OracleClient….ect.
FROM:
Northwind---open with xml
config
<edmx:StorageModels>
<Schema Namespace="NORTHWNDModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2005" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl">
…… </edmx:StorageModels>
NativeSQLCommad Execution.
using (NorthwindEntities
context=new NorthwindEntities())
{
context.Connection.Open();
int
Produt = context.ExecuteStoreCommand("update MyPerson set
ssname='pavanisss' where pid=20", null);
Console.Read();
}
Stored Procedure execution is more simpler then compare to first EDM.(vs 2008 sp1)
TotalProducts(conceptual name)----> totalproducts(acutal name sqlserver)
ObjectResult<Product>
proudt=context.TotalProducts(); // exec totalproducts
foreach (Product item in proudt)
{
Console.WriteLine(item.ProductName);
Console.WriteLine(item.Category.CategoryName);
Console.WriteLine(item.Supplier.CompanyName);
}
Incae of Stored Procedure Requeries Parameters :
ObjectResult<Product> proudt=context.ByProductName("Chai");; // exec totalproducts
}
Incae of Stored Procedure Requeries Parameters :
ObjectResult<Product> proudt=context.ByProductName("Chai");; // exec totalproducts
foreach (Product item in proudt)
{
Console.WriteLine(item.ProductName);
Console.WriteLine(item.Category.CategoryName);
Console.WriteLine(item.Supplier.CompanyName);
}
Designer automatically generates Object parameters:
public ObjectResult<Product> ByProductName(global::System.String productname)
{
ObjectParameter productnameParameter;
if (productname != null)
{
productnameParameter = new ObjectParameter("productname",
productname);
}
else
{
productnameParameter = new ObjectParameter("productname",
typeof(global::System.String));
}
return base.ExecuteFunction<Product>("ByProductName",
productnameParameter);
}
In case of Stored Parameters returns
Complex types(anamouns type): few columns only.
Complex types(anamouns type): few columns only.
then we uses this complex type in code
VS3.5sp1: ObjectContext
vs 2010(.net 4.0) ObjectContext
using (NorthwindEntities
context=new NorthwindEntities())
{
Student sobj = new Student {
sname = "babu", sloc = "hyd" };
context.Students.AddObject(sobj);
context.SaveChanges();
Console.Read();
}
Delete a object :
using (NorthwindEntities
context=new NorthwindEntities())
{
Student sobj =
context.Students.First(i => i.sname == "babu");
context.Students.DeleteObject(sobj);
context.SaveChanges();
Console.Read();
}
No comments:
Post a Comment