0% found this document useful (0 votes)
6 views

Entity Framework V2

Uploaded by

marwanelgammal55
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Entity Framework V2

Uploaded by

marwanelgammal55
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 48

C

#
Entity Framework
Desktop Application
Learning Outcomes

01 02
ORM EF Overview

03 04
Reading Data with CRUD Operations
EF by EF
ADO .Net

2
In this method to deal with database
3
we needed to:
1. Open a connection between VS
4 project and SQLServer and certain
the database.
2. Write the query string.
3. Assign the query result to an
adapter.
4. Make a data table to fill the
records in it then map it into
DataGrid.
Is there an easier way?
0
ORM 1
ORM
Object–relational mapping (ORM, O/RM, and O/R mapping tool) in computer
science is a programming technique for converting data between a relational
database and the heap of an object-oriented programming language. This creates,
in effect, a virtual object database that can be used from within the programming
language.
0
EF Overview2
EF Overview
➢ Entity Framework is an open-source ORM framework for .NET applications
supported by Microsoft.
➢ It enables developers to work with data using objects of domain specific
classes without focusing on the underlying database tables and columns
where this data is stored.
➢ With the Entity Framework, developers can work at a higher level of
abstraction when they deal with data, and can create and maintain data-
oriented applications with less code compared with traditional applications.

Official Definition: “Entity Framework is an object-relational mapper


(O/RM) that enables .NET developers to work with a database
using .NET objects. It eliminates the need for most of the data-access
code that developers usually need to write.”
Entity Framework fits between the
business entities (domain classes) and
the database. It saves data stored in
the properties of business entities and
also retrieves data from the database
and converts it to business entities
objects automatically.
0
Reading Data3
with EF
Reading Data with EF

Right click on
ExpenseIt
project
Reading Data with EF

Add New Item


Choose ADO.NET
Reading Data with EF Entity Data Model

Select Data

Name the
Model
Reading Data with EF

Choose EF Designer
from database to create
a model based on an
existing database.
Reading Data with EF
Reading Data with EF
Reading Data with EF
Reading Data with EF

Make sure to
select the
database
tables
Reading Data with EF

Mapping the
database table into
the Visual Studio
project
Reading Data with EF

There is the model


diagram which shows
design of tables

Context file which


define the entities

Here we can find the


ExpenseReport table
converts into class
(entity)
The context
Reading Data with EF
class is a
most If we open
important Model1.Context.
class while cs file we will see
working with this:
EF 6 or EF
Core. It
represent a The ExpenseEntities class
session with is derived from DbContext,
the which makes it a context
underlying class. It also includes an
database entity set for
using which ExpenseReport.
you can
perform
Create an entity
CRUD
with
(Create,
ExpenseReport
Read,
type.
Update,
Delete)
operations.
If we open
Reading Data with EF ExpenseReport.cs
file we will see how
the columns
An entity in Entity convert into
Framework is a class properties in class
that maps to a (entity).
database table. This
class must be
included as a
DbSet<TEntity>
type property in the
DbContext class. EF
API maps each entity
to a table and each
property of an entity
to a column in the
database.
Reading Data with EF Now let’s show the
report data in
ExpenseReport
page using the
Entity Framework

In the ExpenseReportPage
constructor:
1. we will comment the call of
showTheReport() method
which is used ADO.NET way.
2. Call showByEntity() method
which we will create.
Reading Data with EF
Create
ExpenseEntities
object (db) which
represents the
database

● Here we will bind the result to the DataGrid control by ItemsSource


property.
● We assign it with the list of records that were retrieved using Lambda
Expression.
● The Lambda Expression here means in the database (db) in
ExpenseReports table get the values Where column Names ==
_person.Name in list.
Now let’s bind the
label control for
Reading Data with EF Department

In the first line we will get values from column Department where
column Names == _person.Name, But if we bind the result directly
with the label it will cause a logical error.
Because where get multiple records and we want to assign these
records to a label content which should has a single value.

So we assigned the result to rec variable then bind the label content
with it using FirstOrDefault() method.
FirstOrDefault() get one record only which is the first one.
Output…

IMAGE
0
CRUD 1
Operations by
EF
CRUD Operations by EF
At first we will create a Modify page like this
15
minutes
Activity

Can you make the UI for this


page?
We want to navigate Modify page with Home and
Report page using these buttons
5
minutes
Activity

Do the pages navigation


CRUD Operations by EF Now we want all values appear when
we open the Modify page

So we will write this line in


ModifyRecords constructor.
In this line we bind all data from
ExpenseReports table with the
DataGrid.
CRUD Operations by EF

Add Record
To add record we should
handle this operation in the
code behind by creating a
method that should execute
whenever we click on Add
button.
Create a method with
CRUD Operations by EF name AddRecord

Let’s start with these


lines:
Create an object with
ExpenseReport type,
then assign each
property with the text
boxes values
CRUD Operations by EF

Add the object to the


database using Add()
method.
Don’t forget use
SaveChanges()
method to ensure that
is the records saved in
database.
Let’s try to add some
records
CRUD Operations by EF

Update Record
To update record we should
handle this operation in the
code behind by creating a
method that should execute
whenever we determine the
ID and click on the Update
button
Create a method with
CRUD Operations by EF name UpdateRecord

1. Create an object
with
ExpenseReport
type

2. Get the ID from


the TextBox and
assign it to the
idFromTxt
variable
3. We used Lambda
Expression to get the
record that we want to
update.
First() method will get
the first record that has
the same id was
determined.
CRUD Operations by EF

Same process like


the add method

To update the
result use
AddOrUpdate()
method
Let’s try to update a record
CRUD Operations by EF

Delete Record
To update record we should
handle this operation in the
code behind by creating a
method that should execute
whenever we determine the
ID and click on the Delete
button
CRUD Operations by EF

Create a method with


name DeleteRecord

Here we used Remove() method to


Don’t forget use
delete the record that has the same
SaveChanges()
determined id
method to ensure that is
the records deleted in
database.
Let’s try to delete a record
CRUD Operations by EF

We made the Refresh


button to see the records
after each operation.
Let’s see the code-behind.
CRUD Operations by EF
Create a method with
name Refresh

Then assign all records


to the DataGrid
Output…

IMAGE
THANK YOU!

You might also like