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.