views:

12

answers:

0

Hello,

Let's say I've one model HierarchyData and 2 tables (Organizations and Hierarchies). The Organization table contains info on individual Organization while the Hierarchies table contains only int values (identity of each organization) that represents how thos organizations depend on each other.

(i)I need to create a group of organizations, (ii) send back ID for each row inserted, (iii) use those IDs to create a row in the hierarchy rable.

HierarchyData class is used to move data around

public class HierarchyData
{
  public HierarchyDataInfo Level1 { get; set; }
  public HierarchyDataInfo Level2 { get; set; }
  // and so on

  public HierarchyData()
  {
    Level1 = new HierarchyDataInfo();
    Level2 = new HierarchyDataInfo();
  }
 }
 public class HierarchyDataInfo
 {
   public int OrganizationID { get; set; }
   public string OrganizationName { get; set; }
 }

CreateOrganizationAndSendBackID, which is a SPROC that allow me to create a row in my Organizations Table, then send back the ID of the row that's just been created back to the caller method using Linq.

CREATE PROCEDURE CreateOrganizationAndSendBackID
DECLARE @OrganizationName nvarchar (100),
        @OrganizationID int OUTPUT
AS 
INSERT INTO Organizations (OrganizationName)
       VALUES             (@OrganizationName)
SELECT @OrganizationID = SCOPE_IDENTITYT()

Question: How do I retrieve the @OrganizationID value from my Repository?

This is what I tried, but It didn't work

public void Save()
{
  Hierarchy hierarchy = new Hierarchy();

  if(string.IsNullOrEmpty(HierarchyData.Level1.OrganizationName))
    CreateOrgAndUpdateID(HierarchyData.Level1);
  if(string.IsNullOrEmpty(HierarchyData.Level2.OrganizationName))
    CreateOrgAndUpdateID(HierarchyData.Level2);
  //and so on

  db.Hierarchies.InsertOnSubmit(hierarchy);
  db.SubmitChanges();
}
private void CreateOrgAndUpdateID(HierarchyDataInfo hierarchyDataInfo)
{
 int? myID = 0;
 db.CreateOrganizationAndSendBackID(hierarchyDataInfo.OrganizationName, 
                                    ref myID);
 hierarchyDataInfo.OrganizationID = myID ?? 0;
}

When I execute all this code, rows are inserted in the Organizations table (depending on the OrganizationName that are no null). In the Hierarchy table, a row is also added, but all the columns are null.

That's lengthy question, but I can provide more info to make it clear if that's not the case.

Thanks for helping.