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


Sunday, November 6, 2016

C# ASP.NET Web Forms: Create Custom Event in Web User Control and Subscribe to It

Hello guys,

I tried this to add a custom event to a UserControl and Subscribed to it from the main page containing the UserControl, it is very easy little steps, but very helpful, you will find below screen shots, and an attached solution file as a sample

  1. In the user control before the class declare an event as follows "public event EventHandler eventname;"
  2. Raise the event in whatever event you want as follows "if(eventname != null) this.eventname(object, e);"




(001. Solution Files) 

(002. User Control UI)

User Interface for the UserControl - a simple drop down that when changed it raise the drpChanged event in the user control
(003. User Control Code)



  1.  in the MainPage,  drag the user control
  2.  open the main page in the design mode - source, and go to the user control and add the attribute "OnEventName=name_of_the_event_handler"
(004. Main Page UI)




 (005. Main Page HTML)


(006. Main Page UI after firing the event)




 You can find the complete c# sample code under Our Slack Channer 
https://itggeeks.slack.com/files/tarek/F2T1J69M4/usercontroleventexample.rar