using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Cgm.DataLayer.EntityClasses;
using Cgm.DataLayer.FactoryClasses;
using Cgm.DataLayer.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
namespace Cgm.DataService.Client
{
public class OperationsDataContainer
{
private readonly Context _context;
public OperationsDataContainer()
{
_context = new Context();
_invoiceActivityLogCollection = new EntityCollection(new InvoiceActivityLog01EntityFactory());
_invoiceActivityLogCollection.DoNotPerformAddIfPresent = true;
_context.Add(_invoiceActivityLogCollection);
}
private IEntityCollection2 _invoiceActivityLogCollection;
public IEntityCollection2 InvoiceActivityLogCollection
{
get { return _invoiceActivityLogCollection; }
set
{
_context.Add(value);
_invoiceActivityLogCollection.AddRange(value);
}
}
public void MergeUpdates(IEntityCollection2 invoiceActivityLogCollection)
{
_context.Add(invoiceActivityLogCollection);
for(int i = 0;i<invoiceActivityLogCollection.Count;i++)
{
InvoiceActivityLog01Entity entityRefreshed = invoiceActivityLogCollection[i] as InvoiceActivityLog01Entity;
Debug.Assert(entityRefreshed != null);
Debug.WriteLine(string.Format("{0}:{1}:{2}", entityRefreshed.InvoiceId, entityRefreshed.IsDirty, entityRefreshed.IsNew));
InvoiceActivityLog01Entity entityRefreshedFromContext = _context.Get(entityRefreshed) as InvoiceActivityLog01Entity;
Debug.Assert(entityRefreshedFromContext != null);
Debug.WriteLine(string.Format("{0}:{1}:{2}", entityRefreshedFromContext.InvoiceId, entityRefreshedFromContext.IsDirty, entityRefreshedFromContext.IsNew));
List<int> matches = _invoiceActivityLogCollection.FindMatches(InvoiceActivityLog01Fields.InvoiceId == entityRefreshedFromContext.InvoiceId);
Debug.Assert(matches.Count==1);
InvoiceActivityLog01Entity entityOriginal = _invoiceActivityLogCollection[matches[0]] as InvoiceActivityLog01Entity;
Debug.Assert(entityOriginal != null);
Debug.WriteLine(string.Format("{0}:{1}:{2}", entityOriginal.InvoiceId, entityOriginal.IsDirty, entityOriginal.IsNew));
Debug.WriteLine(string.Format("{0}:{1}", entityRefreshedFromContext.GetHashCode(), entityOriginal.GetHashCode()));
try
{
entityRefreshedFromContext.InvoiceActivityLog02.AddRange(entityRefreshed.InvoiceActivityLog02);
entityRefreshedFromContext.InvoiceComponent.AddRange(entityRefreshed.InvoiceComponent);
entityRefreshedFromContext.InvoiceActivityLog.AddRange(entityRefreshed.InvoiceActivityLog);
}
catch (Exception except)
{
Debug.WriteLine(except);
}
}
}
}
}
The OperationsDataContainer creates a context and a collection when it is created.
On the first retrieval, I set the collection using the property InvoiceActivityLogCollection
Then on the individual entity fetc I call MergeUpdates.
The codes pretty messy right now because I expected it to just need the context, and I've been messing with it to try to get it to work.
a IEntityView is created off of InvoiceActivityLogCollection
and bond to a Devx winforms grid. I'd like the child collections data to be available though it id fetched.