Entity Framework 4 Presentation

Posted Thursday, February 25, 2010 11:40 AM by Nathan Zaugg

Silverlight Logo Small This is a pretty quick post with the notes and screencast from the training I did for an internal company training.  I also planed to give this presentation at the NUNUG meeting in February but my new baby girl decided that she wanted to come that day.

Remember, this is Beta software and can change from the actual release.  Also, I do not work for Microsoft so if I did something incorrectly, I apologize! :)

// Entity Client & Entity SQL
using (EntityConnection conn = new EntityConnection("Name=UtahCodeCampEntities"))
{
    conn.Open();
    EntityCommand cmd = conn.CreateCommand();
    cmd.CommandText = @"SELECT VALUE p
        FROM UtahCodeCampEntities.Presentations AS p 
        WHERE P.EventID = @EventID";
    cmd.Parameters.AddWithValue("EventID", 1);

    DbDataReader rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
    while (rdr.Read())
        Console.WriteLine(rdr["Title"]);
}

// Object Services & Entity SQL
using (UtahCodeCampEntities data = new UtahCodeCampEntities())
{
    ObjectQuery<Presentation> presentations = data.CreateQuery<Presentation>(
        "SELECT VALUE p FROM Presentations AS p WHERE p.User.UserID = @UserId",
        new ObjectParameter("UserID", 5));

    foreach (Presentation presentation in presentations)
        Console.WriteLine(presentation.Title);
}

// Lazy Loading with Object Services
using (UtahCodeCampEntities data = new UtahCodeCampEntities())
{
    ObjectQuery<Presentation> presentations = data.CreateQuery<Presentation>(
        "SELECT VALUE p FROM Presentations AS p");

    foreach (Presentation presentation in presentations)
    {
        if (!presentation.UserReference.IsLoaded)
            presentation.UserReference.Load();

        Console.WriteLine(string.Format("{0} Suggested the topic: {1}",
            presentation.User.UserName,
            presentation.Title));
    }
}

// Eager Loading with Object Services
using (UtahCodeCampEntities data = new UtahCodeCampEntities())
{
    ObjectQuery<Presentation> presentations = data.CreateQuery<Presentation>(
        "SELECT VALUE p FROM Presentations AS p").Include("User");

    foreach (Presentation presentation in presentations)
        Console.WriteLine(string.Format("{0} Suggested the topic: {1}",
            presentation.User.UserName,
            presentation.Title));
}
// LINQ to Entities with Lazy Loading
using (UtahCodeCampEntities data = new UtahCodeCampEntities())
{
    var presentations = from p in data.Presentations
                        where p.Title.Length > 30
                        select p;
    foreach (Presentation presentation in presentations)
        Console.WriteLine(string.Format("{0} Suggested the topic: {1}",
            presentation.User.UserName,
            presentation.Title));
}

// LINQ to Entities with Eager Loading
using (UtahCodeCampEntities data = new UtahCodeCampEntities())
{
    var presentations = from p in data.Presentations.Include("User")
                        where p.Title.Length > 30
                        select p;
    
    foreach (Presentation presentation in presentations)
        Console.WriteLine(string.Format("{0} Suggested the topic: {1}",
            presentation.User.UserName,
            presentation.Title));
}

// POCO Insert New Object & Transaction Support
User u = new User();
u.UserName = "JohnDoe999";
u.PassHash = "APasswordHere";
u.FirstName = "John";
u.LastName = "Doe";
u.EmailAddress = "JDoe@compserv.com";
u.ShirtSize = "XL";
u.CreatedDate = DateTime.Today;

using (UtahCodeCampEntities data = new UtahCodeCampEntities())
{
    DbTransaction trans = data.Connection.BeginTransaction();
    data.Users.AddObject(u);
    data.SaveChanges();
    trans.Commit();
}

// POCO Update Object
User usr = null;
using (UtahCodeCampEntities data = new UtahCodeCampEntities())
{
    usr = data.Users.Where(n => n.UserName == "JohnDoe999").Single();
}

// Send user over WCF -- Simulated Disconnected Scenerio
usr.ShirtSize = "M";

// Return back to server
using (UtahCodeCampEntities data = new UtahCodeCampEntities())
{
    data.Users.ApplyChanges(usr);
    data.SaveChanges();
}

I hope this helps someone who is looking for some answers on how to do stuff.

Filed under: ,