Wednesday, November 23, 2016

Entity Frame Work: CRUD in disconnected mode


EF Architecture


In this post we will show how to do your CRUD operations in disconnected mode.

Prerequisites:
1- C# programming
2- Basic knowledge of how to start with "Entity Framework" - EF


First we have to know what we mean by "disconnected mode", disconnected mode means that you receive the data model from a different tier than your current db context.

Ex. You got a "Student" object from a "web service" or "web api" and you need to insert or update this student in your database.

Operations:
1- Create / Insert record

a- "DBSet.Add()" add new object "graph" with automatically applying "added" to its state 

DbSet.Add() - State Changed



b- "DBSet.Attach()" add new object "graph" without applying a change to its state

DbSet.Attach() - State Not Changed

 c- "DbSet.Add() / "context.SaveChanges() - Adding just the entity

DbSet.Add()/context.SaveChanges() - Saving just the entity without graph

or alternatively we can use the "context.Entry() add mark it as added

context.Entry() / context.SaveChanges() - Saving Just the Entity



2- Update
To modify an entity outside the context scope and then update it into the database, we use the following:

a- Make the modification in our entity
b- Pass the modified entity into the entry method and marks its state as Modified
c- Call SaveChanges() method to update entity information

Update Entity - Disconnected Mode



3- Delete
Like the previous "Update" example, we will use context.Entry() method to delete an entity in disconnected mode

Delete Entity - Disconnected Mode

For deeper look into Entity Framework you can visit this link http://www.entityframeworktutorial.net/EntityFramework5/entity-framework5-introduction.aspx


No comments:

Post a Comment