Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Let's say I have two lists in my web site, linked by a lookup :

  • List A (content type ACT)
    • Title (text)
  • List B (content type BCT)
    • Title (text)
    • A (lookup to A)

I use SPMetal to generate a DataContext. SPMetal will generates class like this (pseudo code for readability) :

public class ACT
{
     public string Title { get; set;}
     public EntityList<BCT> B { get; set; }
}

public class BCT
{
     public string Title { get; set;}
     public EntityRef<ACT> A { get; set; }
}

and that's OK.

Now I want to add some B entities to on existing A object. If I use :

using(var ctx = new MyDataContext())
{
     var myA = ctx.A.Where(a=>a.Id = theIdFromSomewhere).Single();

     var newB = new BCT{
         A = myA,
         Title = "I am your father"
     }

     myA.B.Add(newB);

     ctx.SubmitChanges();
}

This code won't throw any exception, but the newB entity won't be created on the sharepoint site.

If I want to fix, I have to add ctx.B.Add(newB) just before the call to submitchanges. And that fix the issue.

However, while it's very simple in my case because I do have the context available, how can I automatically mark new child entities to be inserted?

To illustrate my issue, imagine I'm somewhere in a deep nested sequence of utility method that waits for a ACT parameter :

public static void MyVeryUsefulMethod(ACT myA)
{
     var newB = new BCT{
         A = myA,
         Title = "You don't know the power of the Dark Side"
     }

     myA.B.Add(newB);
}

In this case, I don't have access to the context object.

I can see some workarounds :

  1. Return the newly created newB object by changing the method signature to public static BCT MyVeryUsefulMethod(ACT A) and let the caller add the new entity object to the context. This works, but I still have to flow the newly created object through the whole stack until I reach the method that created the context.
  2. pass the context through all methods calls (public static void MyVeryUsefulMethod(MyDataContext ctx, ACT A). This can also works, but a lot of noisy parameters in my specific object model.
  3. Inject the Context as a property of generated classes. I don't know how to it unfortunately
  4. Store the context object in [ThreadStatic] static variable wrapped in a static property. This may works, but seems a bit tweaky
  5. throw off SPMetal into the void and write my own mapping framework. This looks like seductive, as the more I use SPMetal the more I feel it's unreliable (especially when working with content types in VS). In fact, I think I'm not using the right tool. SPMetal looks like more suited for client/server scenario than for custom application coding.

I'd appreciate you share your feelings and experience.

share|improve this question
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.