Materializing an sproc result into POCO Entities RRS feed

  • Question

  • I have a business object that needs two collections of strongly-typed results that are returned from a stored procedure. A lot of the examples I've seen online have to do with the parent being an Entity, but in my case, the business object does not belong in the context.

    The assignments are just a POCO class, nothing special. Just some strings and ints. All of my real context-related DAL stuff is in a db-first generated edmx (with 4.1 DbContext T4 templating and some extra repository/unit of work classes)

    The parent object "AssignmentResult" should just be a business object with a List of Assignments and a separate Assignment property.

    I am using EF Extensions and everything executes correctly, but I can't get the Materialize<T> method (or any other) to do anything but return a collection of the correct number of items, but whose values are all null.

    This happens if I use Materialize<T> or even go  down to the ObjectContext and use Translate<T> as well.

    I've tried this with and without deferred loading.

    Here's some code:

    ObjectContext objectContext = ((IObjectContextAdapter)this).ObjectContext;
          AssignmentResult result = new AssignmentResult();
          SqlParameter reportIDParameter = new SqlParameter
                               ParameterName = "@ReportID",
                               DbType = DbType.Int16,
                               Value = reportID //set earlier
          IEnumerable<Assignment> test;
          DbCommand cmd = objectContext.CreateStoreCommand("GetAssignments", CommandType.StoredProcedure, reportIDParameter);
          using (var connectionScope = cmd.Connection.CreateConnectionScope())
            using (var reader = cmd.ExecuteReader())
              //neither of these work
              assigned = cmd.Materialize<Assignment>();
              assigned = objectContext.Translate<Assignment>(reader);
              result.Assigned = test;
              var unassigned = objectContext.Translate<Assignment>(reader);
              if (unassigned.Count() > 0)
                result.Unassigned = unassigned.Single();

    I've also gone down the awful road of trying to attach to the current ObjectContext but then I get the error "no mapping or metadata found" because I don't think the context is auto-magically generating an EntitySet for Assignments. 

    The only time I was ever able to pull back data that wasn't null in this app is when I used an ObservableCollection<T> in another place and messed around with .Current and Load(). Am I gonna have to do that here? Also, once I get data, when I go to Add() the results from the Db into the List object, will it freak out?



    Tuesday, July 5, 2011 10:25 PM


  • Stupid mistake. In my Assignment class I forgot to add {get; set;}. Doh!
    • Marked as answer by j-simms Tuesday, July 5, 2011 10:50 PM
    Tuesday, July 5, 2011 10:50 PM