Intro to C#.net and EF Ilan Shimshoni. The Three Faces of ADO.NET The connected layer – Directly...
-
Upload
angelica-mcbride -
Category
Documents
-
view
212 -
download
0
Transcript of Intro to C#.net and EF Ilan Shimshoni. The Three Faces of ADO.NET The connected layer – Directly...
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
• Connection through the Entity Framework (EF)– Automatically defining the relation between the
DB and the C# classes and working with LINQ.
The connected layer
.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
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(); } }
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; }
DataSets
Client Application
DataSet Data Adapter Database
DataSets
• Represents a database in memory• Includes:– Tables– Relationships– Tables in include columns (fields)
• Constraints• Name• Datatype• Unique?• Column number (ordinal)
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
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); }
The Entity Framework
The third layer
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.
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)
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
*.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>
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>
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>