Intro to C#.net and EF Ilan Shimshoni. The Three Faces of ADO.NET The connected layer – Directly...

17
Intro to C# .net and EF Ilan Shimshoni

Transcript of Intro to C#.net and EF Ilan Shimshoni. The Three Faces of ADO.NET The connected layer – Directly...

Page 1: Intro to C#.net and EF Ilan Shimshoni. The Three Faces of ADO.NET The connected layer – Directly connecting to the DB The disconnected layer – Using datasets.

Intro to C# .net and EF

Ilan Shimshoni

Page 2: Intro to C#.net and EF Ilan Shimshoni. The Three Faces of ADO.NET The connected layer – Directly connecting to the DB The disconnected layer – Using datasets.

The Three Faces of ADO.NET

• The connected layer– Directly connecting to the DB

• The disconnected layer– Using datasets

• Connection through the Entity Framework (EF)– Automatically defining the relation between the

DB and the C# classes and working with LINQ.

Page 3: Intro to C#.net and EF Ilan Shimshoni. The Three Faces of ADO.NET The connected layer – Directly connecting to the DB The disconnected layer – Using datasets.

The connected layer

Page 4: Intro to C#.net and EF Ilan Shimshoni. The Three Faces of ADO.NET The connected layer – Directly connecting to the DB The disconnected layer – Using datasets.

.Net Platform Data Provider

Database

ClientAssembly

Connection Object

Connection Object

DataReader Object

DataAdaptor Object

Transaction

Parameter Collection

Select Command

Update Command

Delete Command

Insert Command

Page 5: Intro to C#.net and EF Ilan Shimshoni. The Three Faces of ADO.NET The connected layer – Directly connecting to the DB The disconnected layer – Using datasets.

Insert Method public void InsertAuto(NewCar car) { // Format and execute SQL statement. string sql = string.Format("Insert Into Inventory" + "(CarID, Make, Color, PetName) Values" + "('{0}', '{1}', '{2}', '{3}')", car.CarID, car.Make, car.Color, car.PetName);

// Execute using our connection. using (SqlCommand cmd = new SqlCommand(sql, this.sqlCn)) { cmd.ExecuteNonQuery(); } }

Page 6: Intro to C#.net and EF Ilan Shimshoni. The Three Faces of ADO.NET The connected layer – Directly connecting to the DB The disconnected layer – Using datasets.

Select Method public List<NewCar> GetAllInventoryAsList() { // This will hold the records. List<NewCar> inv = new List<NewCar>();

// Prep command object. string sql = "Select * From Inventory"; using (SqlCommand cmd = new SqlCommand(sql, this.sqlCn)) { SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { inv.Add(new NewCar { CarID = (int)dr["CarID"], Color = (string)dr["Color"], Make = (string)dr["Make"], PetName = (string)dr["PetName"] }); } dr.Close(); } return inv; }

Page 7: Intro to C#.net and EF Ilan Shimshoni. The Three Faces of ADO.NET The connected layer – Directly connecting to the DB The disconnected layer – Using datasets.

DataSets

Client Application

DataSet Data Adapter Database

Page 8: Intro to C#.net and EF Ilan Shimshoni. The Three Faces of ADO.NET The connected layer – Directly connecting to the DB The disconnected layer – Using datasets.

DataSets

• Represents a database in memory• Includes:– Tables– Relationships– Tables in include columns (fields)

• Constraints• Name• Datatype• Unique?• Column number (ordinal)

Page 9: Intro to C#.net and EF Ilan Shimshoni. The Three Faces of ADO.NET The connected layer – Directly connecting to the DB The disconnected layer – Using datasets.

DataSets

• When the dataset is modified• The modifications are known• Errors are detected• They can be written back to the database

using the DataAdapter• They can be rolled back

Page 10: Intro to C#.net and EF Ilan Shimshoni. The Three Faces of ADO.NET The connected layer – Directly connecting to the DB The disconnected layer – Using datasets.

Datasets Gui and LinQ private void CreateDataView2() { dataGridYugosView.DataSource = from car in inventoryTable where (string)car["Make"] == "Yugo" select car;

dataGridYugosView.DataError += new DataGridViewDataErrorEventHandler(DataGridView1_DataError); }

Page 11: Intro to C#.net and EF Ilan Shimshoni. The Three Faces of ADO.NET The connected layer – Directly connecting to the DB The disconnected layer – Using datasets.

The Entity Framework

The third layer

Page 12: Intro to C#.net and EF Ilan Shimshoni. The Three Faces of ADO.NET The connected layer – Directly connecting to the DB The disconnected layer – Using datasets.

EF

• Separate the strong link between the DB and the program

• Build strongly typed objects classes to represent the DB in the program. These objects are called the Entity Data Model(EDM)

• A set of XML files called edmx files connect between the DB and the EDM classes.

Page 13: Intro to C#.net and EF Ilan Shimshoni. The Three Faces of ADO.NET The connected layer – Directly connecting to the DB The disconnected layer – Using datasets.

EF components

• Object Services: – all EDM classes inherit from EntityObject. Its in charge

of monitoring modifications to the object in order to then update the corresponding DB record(s)

• EntityClient namespace:– Its like a data provider but fro EDM objects.

• *.edmx files:– Conceptual model (Describe EDM classes)– Physical model (Describe DB tables and rels)– Logical model (relates the EDM to the DB tables)

Page 14: Intro to C#.net and EF Ilan Shimshoni. The Three Faces of ADO.NET The connected layer – Directly connecting to the DB The disconnected layer – Using datasets.

All Together Now

C# Code

Object Services

Entity Client Data Provider

ADO.NET Data Provider

Physical Database

DbDataReader

EntityDataReader

*.edmx

IEnumerable<T>LINQ QueryEntity SQLEntity SQL

Command Tree

Command Tree

Page 15: Intro to C#.net and EF Ilan Shimshoni. The Three Faces of ADO.NET The connected layer – Directly connecting to the DB The disconnected layer – Using datasets.

*.edmx files: DB Storage<edmx:StorageModels> <Schema Namespace="AutoLotModel.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"> <EntityContainer Name="AutoLotModelStoreContainer"><EntitySet Name="Inventory" EntityType="AutoLotModel.Store.Inventory" store:Type="Tables" Schema="dbo" /><EntityType Name="Inventory"> <Key> <PropertyRef Name="CarID" /> </Key> <Property Name="CarID" Type="int" Nullable="false" /> <Property Name="Make" Type="varchar" Nullable="false" MaxLength="50" /> <Property Name="Color" Type="varchar" Nullable="false" MaxLength="50" /> <Property Name="PetName" Type="varchar" MaxLength="50" /> </EntityType></Schema></edmx:StorageModels>

Page 16: Intro to C#.net and EF Ilan Shimshoni. The Three Faces of ADO.NET The connected layer – Directly connecting to the DB The disconnected layer – Using datasets.

Conceptual Model (EDM)<edmx:ConceptualModels> <Schema Namespace="AutoLotModel" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2008/09/edm"> <EntityContainer Name="AutoLotEntities" annotation:LazyLoadingEnabled="true"><EntitySet Name=“Cars" EntityType="AutoLotModel.Car" /> <EntityType Name="Inventory"> <Key> <PropertyRef Name="CarID" /> </Key> <Property Name="CarID" Type="Int32" Nullable="false" /> <Property Name="Make" Type="String" Nullable="false" MaxLength="50" Unicode="false" FixedLength="false" /> <Property Name="Color" Type="String" Nullable="false" MaxLength="50" Unicode="false" FixedLength="false" /> <Property Name=“CarNickname" Type="String" MaxLength="50" Unicode="false" FixedLength="false" /></Schema> </edmx:ConceptualModels>

Page 17: Intro to C#.net and EF Ilan Shimshoni. The Three Faces of ADO.NET The connected layer – Directly connecting to the DB The disconnected layer – Using datasets.

C-S mapping<edmx:Mappings> <Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2008/09/mapping/cs"> <EntityContainerMapping StorageEntityContainer="AutoLotModelStoreContainer" CdmEntityContainer="AutoLotEntities"> <EntitySetMapping Name=“Cars"><EntityTypeMapping TypeName="AutoLotModel.Car"><MappingFragment StoreEntitySet="Inventory"> <ScalarProperty Name="CarID" ColumnName="CarID" /> <ScalarProperty Name="Make" ColumnName="Make" /> <ScalarProperty Name="Color" ColumnName="Color" /> <ScalarProperty Name=“CarNickname" ColumnName="PetName" /> </MappingFragment></EntityTypeMapping> </EntitySetMapping></Mapping> </edmx:Mappings>