The Blog of Travis Gneiting
  • Home
KEEP IN TOUCH

Posts in category Entity Framework

Entity Framework Load Related Singleton Reference, Delete

Oct11
2010
Leave a Comment Written by admin

1. Simple Load and Bind Example

Dim AdventureWorks_DataEntity as New AdventureWorksDataEntities()
Dim Query = From e IN AdventureWorks_DataEntity.Employee
Gridview.DataSource = query
Gridview.databind()

2. Load Related Data in Entity Framework

Dim AdventureWorks_DataEntity as New AdventureWorksDataEntities()
For Each employee in AdventureWorks_DataEntity.Employee
Dim Li as New ListItem()
Li.Text = employee.EmployeeID & " "

If (Not employee.EmployeePayHistory.IsLoaded) Then
employee.EmployeePayHistory.Load()
End if

For Each pay In employee.EmployeePayHistory
li.Text &= "Pay Rate: " & pay.Rate & " "
Next pay

BulletedList1.Items.Add(li)
Next employee

Another option for loading related data is to use the “include”. This will eager load the children EmployeePayHistory for the employees.

Dim AdventureWorks_DataEntity as New AdventureWorksDataEntities()
Dim query = from e in employee.Include("EmployeePayHistory")

3. Singleton Reference (One to One) to get relationship use “objectReference”

var personTwo = (from p in context.Person WHERE p.Student != null select p).first();
personTwo.StudentReference.Load()

'(one to Many relationship)
personTwo.Student.Load()

4. Delete Entity

<span style="font-size: xx-small;">try
{
using (var context = new AdventureWorks2008Entities())
{
var prod = context.Products.Where(p => p.ProductID == 1005).First();
context.DeleteObject(prod);
context.SaveChanges();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.InnerException.Message);
}
</span>

5.ATTACHING and DETACHING

If calls across assembly layer, it’s detached when accessed.

<asp:EntityDataSource Include=”Student”>

<asp:listview Id=”listView” runat=”server” DataKeyNames=”PersonID” DataSourceID=”personDataSource” Insert …

<%# Eval(“Student.Year”) %>

<asp:ObjectDataSource (Develop new class i.e. people.cs as partial class)

SchoolEntities context = new SchoolEntities();

return context.Person.INclude(“Student.Where(p => p.enrollmentdate.hasvalue.tolist();

6.POCO

Still to come…

Entity Framework Load Related Entities

Mar16
2010
Leave a Comment Written by admin

Customer.Orders.Load() Load for each object

db.Customer.Include(“Orders”) Loads once for all objects

Programming

  • ASP.NET
  • MSDN
  • Visual Studio Offical Website
  • www.w3.org

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

Welcome to My Programming Blog

This is my brain dump. I use it to post thing I may use again, interesting things I have run into and programming helps.

Tags

.net Agile Software Development Engineering ASP.NET attack Beginner Blueprint CSS Database Deployment DevExpress Framework Functional hacked Hotmail How to test software HTTP IIS Javascript JQuery MIME New Website Checklist PHP querystring Software Testing Specification spoof Status Codes Testing Trace Debug ASP.NET Tutorial Velocity webmethod Website Testing Zend

Blog Archive

  • January 2012
  • November 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • February 2011
  • January 2011
  • November 2010
  • October 2010
  • August 2010
  • July 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • February 2009
  • January 2009
  • November 2008
  • September 2008
  • August 2008
  • July 2008
  • April 2008

EvoLve theme by Theme4Press  •  Powered by WordPress The Blog of Travis Gneiting