0

I have the following code:

ICollection<Sample> samples = new Collection<Sample>();
samples.Add(sample1);
samples.Add(sample2);
Order record = scope.DbContext.Orders.AddNew(new Order
{
    Name = GenerateName("Order"),
    Samples = samples
});

When I look at samples, it contans both sample1 and sample2. However, when I add it to Order's Samples , Samples is empty. Samples is also of type ICollection. How can I add an ICollections to an empty ICollection?


Update:

Results when I print samples and record.Samples in intermediate:

samples
Count = 2
    [0]: {iVention.Lifescience.Test.ElectronicSignatureRepositoryTest.SignHierarchyTest.Sample one.2012-04-21 11:07:24}
    [1]: {iVention.Lifescience.Test.ElectronicSignatureRepositoryTest.SignHierarchyTest.Sample two.2012-04-21 11:07:24}
record.Samples
{System.Data.Objects.DataClasses.EntityCollection<iVention.Database.Sample>}
    [System.Data.Objects.DataClasses.EntityCollection<iVention.Database.Sample>]: {System.Data.Objects.DataClasses.EntityCollection<iVention.Database.Sample>}
    Count: 0
    IsReadOnly: false

Update 2:

AddNew onlu adds the entity, why would this remove the samples from Samples?

public static TEntity AddNew<TEntity>(this IDbSet<TEntity> self, object fromValues) where TEntity : class
{
    return self.Add(Create(self, fromValues));
}

in object formValues given to AddNew, Samples still contains 2 samples.

Add looks like this:

public static TEntity Create<TEntity>(this IDbSet<TEntity> self, object fromValues) where TEntity : class
{
   TEntity returnValue = self.Create();
   var targetProperties = typeof(TEntity).GetProperties();
   {
   var targetProperty = targetProperties.Single(c => c.Name == property.Name);
   if (targetProperty.CanWrite && property.GetIndexParameters().Length == 0)
   {
      if (targetProperty.PropertyType.IsGenericType && targetProperty.PropertyType.GetGenericTypeDefinition() == typeof(System.Collections.Generic.ICollection<>))
      {
      }
      else
      {
         targetProperty.SetValue(returnValue, property.GetValue(fromValues, null), null);
      }
   }
   return returnValue;
}

So nothing gets added to returnValue when it is an ICollection. Samples in returnValue is empty.

Niek de Klein
  • 8,524
  • 20
  • 72
  • 143
  • Does method scope.DbContext.Orders.AddNew() return the same instance of Order, that you pass as it's argument? – Ňuf Apr 21 '12 at 10:41
  • Think so, it contains the right Name. – Niek de Klein Apr 21 '12 at 10:45
  • Try to omit AddNew method: Order record = new Order {Name = GenerateName("Order"),Samples = samples}); If it helps, there is something wrong with AddNew, in not, i would suspect Samples property of Order class. – Ňuf Apr 21 '12 at 10:57
  • oh yeah, that works, I'll look if I can find something in AddNew – Niek de Klein Apr 21 '12 at 11:12
  • try out `record[0].Samples` or `record.Last().Samples` – sll Apr 21 '12 at 11:22
  • record[] has to get a string. And it does not have a Last(). – Niek de Klein Apr 21 '12 at 11:27
  • Update2 is not very helpful, because it doesn't show what Create() and self.Add() methods do. They probably do something else, than you expect, try step through them in debugger. – Ňuf Apr 21 '12 at 11:27

0 Answers0