Insert a row in database using EntityFramework 3.5
public abstract class EntityObject : StructuralObject,
IEntityWithKey, IEntityWithChangeTracker,
IEntityWithRelationships
{
public EntityKey EntityKey { get;
set; }
public EntityState EntityState { get;
}
protected override sealed void ReportPropertyChanged(string
property);
protected override sealed void ReportPropertyChanging(string
property);
}
Every object is derived from EntityObject type.
EntityKey ,
EntityState:Detached,Added,Deleted,Unchanged ;
Delete in edm:
Employees
emp = entitycontext.Employees.FirstOrDefault(emps => emps.FirstName == "kasibabu");
try
{
entitycontext.DeleteObject(emp); // gives error if emp is null.
entitycontext.SaveChanges();
}
catch
(Exception)
{
Console.Write("object is not found");
}
First---------> database must contains with the specified condition.
FirstOrDefault---> database may or may not satisfies condition.
Update in edm:
Employees emp = entitycontext.Employees.First(emps => emps.FirstName == "kasi");
emp.FirstName = "kasibabu";
entitycontext.SaveChanges();
Update :
Employees
emp = entitycontext.Employees.First(emps => emps.FirstName == "kasi");
emp.FirstName = "kasibabu";
entitycontext.SaveChanges();
Note: for adding and deleteing object can be perform from EDM 4.0 onwords like below also
entitiycontext.Employee.AddObject(empobj);
and
entitycontext.Employee.DeleteObject(empobj);
as well as
good explanation about Entity Framework. and EF- about SSDL,CSDL,MSL
Entity framework has a conceptual model which is not directly linked to the domain model as in case of LINQ to SQL. This enables us to have a mapping layer in between the conceptual model and the persistent model. Entity framework uses 3 layers(EntityClient)
- SSDL – Store schema definition language. This is the logical model which stores the database objects. This layer contains the Tables, Views, Stored Procedure, Functionsetc. These objects can be queried using SqlCommand, SqlConnection, SqlDataAdapteretc.
- CSDL – Conceptual Schema definition language. This is the object model or the entity model that we use to query the data. This layer consists of Container, EntitySets, AssociationSets, AssociationTypes, Functions etc. These objects can be queried using an ADO.NET provider which exposes objects like EntityConnection, EntityCommand, EntityDataReader using a language called Entity SQL (ESQL).
- MSL – Mapping Schema Language. This layer maps the conceptual model with the logical model. We use code attributes to map the conceptual model with the logical model. The following diagram taken from an MSDN article demonstrates the Architecture of Entity Framework.
as well as
at runtime based on the provider--> SSDL
SqlClient / OracleClient/ oledbClient...ect decided by EntityClient. makes connection
like
EntityConnection has a property ---->
EntityConnection:DbConnection
{
public DbConnection StoreConnection { get; } // Storage Schema
}
StoreConnection : decides the derived type objects.
SqlConnection, SqlCommand,SqlDataAdapter............ect
or
OracleConnection,OracleCommand,OracleAdapter....ect
this DbConnection can be OracleConnection and SqlConnection.
SqlConnection : DbConnection, OracleConnection:DbConnection
like
based the stored connection which will connect to apporiate connection objects.
actuallly -->these EntityConnection EntityCommand are works for
actuallly -->these EntityConnection EntityCommand are works for
SqlConnection,SqlCommand.........ect.
every opertion(internally uses) is done through ADO.NET classes only.
as well
these classes are actually works with underlaying ADO.net classes like SQlconnection,SqlDataAdapter,SqlCommand...ect
these are hide by the Entity Framework---Storage Schema Implementation.
References:
http://www.nileshgule.com/2010/09/entity-framework-part-6-perform-crud.htmlevery opertion(internally uses) is done through ADO.NET classes only.
NorthwindEntities context = new NorthwindEntities();
EntityConnection
conn = new EntityConnection(context.Connection.ConnectionString);
conn.Open();
var
sql = "SELECT emp.LastName, emp.FirstName
" +
"FROM
NorthwindEntities.Employees AS emp";
EntityCommand
cmd = new EntityCommand(sql,
conn);
DbDataReader
reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
while
(reader.Read())
{
Console.WriteLine("{0} {1}", reader["LastName"], reader["FirstName"]);
}
as well
these classes are actually works with underlaying ADO.net classes like SQlconnection,SqlDataAdapter,SqlCommand...ect
these are hide by the Entity Framework---Storage Schema Implementation.
References:
http://beaucrawford.net/post/Entity-Framework---Generating-SSDL2c-CSDL2c-and-MSL.aspx
http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2007/08/27/9611.aspx
No comments:
Post a Comment