Displaying and Editing Data by Using the Entity Framework and ...

fishglugΛογισμικό & κατασκευή λογ/κού

13 Δεκ 2013 (πριν από 4 χρόνια και 18 μέρες)

96 εμφανίσεις

Displaying and
Editing
Data by

Using the Entity Framework and

Data Binding

Fehim Korhan YAMAN

2008514024

Entity
Framework and Data Model


Entity Framework is a technology
that is used for
querying and manipulating
databases.



Entity data model is a logical model of a database.



We use the entity framework to generate a logical data model.


R
etriev
ing

I
nformation

and
E
stablish
ing

the
D
ata

B
indings


using System.ComponentModel;


using System.Collections;


public
partial class SupplierInfo : Window


{



private NorthwindEntities northwindContext = null;



private Supplier supplier = null;



private IList productsInfo = null;



...


}

R
etriev
ing

I
nformation

and
E
stablish
ing

the
D
ata

B
indings


private void Window_Loaded(object sender, RoutedEventArgs e)


{



this.northwindContext = new NorthwindEntities();



suppliersList.DataContext = this.northwindContext.Suppliers;


}



private void suppliersList_SelectionChanged(object sender,


SelectionChangedEventArgs e)


{



this.supplier = suppliersList.SelectedItem as Supplier;



this.northwindContext.LoadProperty<Supplier>(this.supplier, s => s.Products);



this.productsInfo = ((IListSource)supplier.Products).GetList();



productsList.DataContext = this.productsInfo;


}

Using LINQ to Entities to Query Data


NorthwindEntities northwindContext = new NorthwindEntities();


ObjectQuery<Product> products = northwindContext.Products;





var productNames = from p in products



select p.ProductName;





foreach (var name in productNames)


{



Console.WriteLine("Product name: {0}", name);


}

Using Data Binding
for Data editing


NorthwindEntities northwindContext = new NorthwindEntities();





try


{



Product product = northwindContext.Products.Single(p => p.ProductID == 14);



product.ProductName = "Bean Curd";



northwindContext.SaveChanges();


}


catch (OptimisticConcurrencyException ex)


{



northwindContext.Refresh(RefreshMode.ClientWins,
northwindContext.Products);



northwindContext.SaveChanges();


}


QUESTIONS
?

References



Microsoft
Visual
C
#

2010
Step by Step