Hi,
I have a simple database model, a Document table and a TransfusionRecord table where TransfusionRecord contains a Document foreign key (one - to - many association). I have a LightSpeed model with a Document class and a TransfusionRecord class with the one - to - many association as well. I add an inheritance association such that TransfusionRecord inherits Document. I receive a circular association error when trying to query the transfusion records to bind to a grid. I call the following function:
public IQueryable<T> Find<T>() where T : Entity
{
return UnitOfWork.Query<T>();
}
The error takes place when calling ToList() on IQueryable<T>.
The SQL to create a simplified database model is:
CREATE TABLE [dbo].[Document](
[Id] [int] NOT NULL,
[ChartId] [int] NOT NULL,
[DocumentTypeId] [int] NOT NULL,
[CreatedOn] [datetime] NOT NULL,
[UpdatedOn] [datetime] NOT NULL,
[DeletedOn] [datetime] NULL,
CONSTRAINT [PK_Document] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[TransfusionRecord](
[Id] [int] NOT NULL,
[DocumentId] [int] NOT NULL,
[PhysicianId] [int] NULL,
[ProductGroupId] [int] NOT NULL,
[DonorNumber] [nvarchar](50) NOT NULL,
[ProductType] [nvarchar](50) NOT NULL,
[Irradiated] [bit] NULL,
[Uncrossmatched] [bit] NULL,
[ReleasedOn] [datetime] NULL,
[CreatedOn] [datetime] NOT NULL,
[UpdatedOn] [datetime] NOT NULL,
[DeletedOn] [datetime] NULL,
CONSTRAINT [PK_TransfusionRecord] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[TransfusionRecord] WITH CHECK ADD CONSTRAINT [FK_TransfusionRecord_Document] FOREIGN KEY([DocumentId])
REFERENCES [dbo].[Document] ([Id])
GO
ALTER TABLE [dbo].[TransfusionRecord] CHECK CONSTRAINT [FK_TransfusionRecord_Document]
GO
The designer generated code is:
[Serializable]
[System.CodeDom.Compiler.GeneratedCode("LightSpeedModelGenerator", "1.0.0.0")]
[System.ComponentModel.DataObject]
[InheritanceMapping(InheritanceMappingKind.ClassTableInheritance)]
public partial class Document : Entity<int>
{
#region Fields
private int _chartId;
private int _documentTypeId;
#pragma warning disable 649 // "Field is never assigned to" - LightSpeed assigns these fields internally
private readonly System.DateTime _createdOn;
private readonly System.DateTime _updatedOn;
private readonly System.Nullable<System.DateTime> _deletedOn;
#pragma warning restore 649
#endregion
#region Field attribute and view names
/// <summary>Identifies the ChartId entity attribute.</summary>
public const string ChartIdField = "ChartId";
/// <summary>Identifies the DocumentTypeId entity attribute.</summary>
public const string DocumentTypeIdField = "DocumentTypeId";
#endregion
#region Relationships
[ReverseAssociation("Document")]
private readonly EntityCollection<Event> _events = new EntityCollection<Event>();
[ReverseAssociation("Document")]
private readonly EntityCollection<TransfusionRecord> _transfusionRecords = new EntityCollection<TransfusionRecord>();
[ReverseAssociation("Documents")]
private readonly EntityHolder<Chart> _chart = new EntityHolder<Chart>();
[ReverseAssociation("Documents")]
private readonly EntityHolder<DocumentType> _documentType = new EntityHolder<DocumentType>();
#endregion
#region Properties
public EntityCollection<Event> Events
{
get { return Get(_events); }
}
public EntityCollection<TransfusionRecord> TransfusionRecords
{
get { return Get(_transfusionRecords); }
}
public Chart Chart
{
get { return Get(_chart); }
set { Set(_chart, value); }
}
public DocumentType DocumentType
{
get { return Get(_documentType); }
set { Set(_documentType, value); }
}
/// <summary>Gets or sets the ID for the <see cref="Chart" /> property.</summary>
public int ChartId
{
get { return Get(ref _chartId, "ChartId"); }
set { Set(ref _chartId, value, "ChartId"); }
}
/// <summary>Gets or sets the ID for the <see cref="DocumentType" /> property.</summary>
public int DocumentTypeId
{
get { return Get(ref _documentTypeId, "DocumentTypeId"); }
set { Set(ref _documentTypeId, value, "DocumentTypeId"); }
}
/// <summary>Gets the time the entity was created</summary>
public System.DateTime CreatedOn
{
get { return _createdOn; }
}
/// <summary>Gets the time the entity was last updated</summary>
public System.DateTime UpdatedOn
{
get { return _updatedOn; }
}
/// <summary>Gets the time the entity was soft-deleted</summary>
public System.Nullable<System.DateTime> DeletedOn
{
get { return _deletedOn; }
}
#endregion
}
[Serializable]
[System.CodeDom.Compiler.GeneratedCode("LightSpeedModelGenerator", "1.0.0.0")]
[System.ComponentModel.DataObject]
[Discriminator(Attribute="DocumentTypeId", Value=21)]
public partial class TransfusionRecord : Document
{
#region Fields
[ValidatePresence]
[ValidateLength(0, 50)]
private string _donorNumber;
[ValidatePresence]
[ValidateLength(0, 50)]
private string _productType;
private System.Nullable<bool> _irradiated;
private System.Nullable<bool> _uncrossmatched;
private System.Nullable<System.DateTime> _releasedOn;
private int _documentId;
private System.Nullable<int> _physicianId;
private int _productGroupId;
#pragma warning disable 649 // "Field is never assigned to" - LightSpeed assigns these fields internally
private readonly System.DateTime _createdOn;
private readonly System.DateTime _updatedOn;
private readonly System.Nullable<System.DateTime> _deletedOn;
#pragma warning restore 649
#endregion
#region Field attribute and view names
/// <summary>Identifies the DonorNumber entity attribute.</summary>
public const string DonorNumberField = "DonorNumber";
/// <summary>Identifies the ProductType entity attribute.</summary>
public const string ProductTypeField = "ProductType";
/// <summary>Identifies the Irradiated entity attribute.</summary>
public const string IrradiatedField = "Irradiated";
/// <summary>Identifies the Uncrossmatched entity attribute.</summary>
public const string UncrossmatchedField = "Uncrossmatched";
/// <summary>Identifies the ReleasedOn entity attribute.</summary>
public const string ReleasedOnField = "ReleasedOn";
/// <summary>Identifies the DocumentId entity attribute.</summary>
public const string DocumentIdField = "DocumentId";
/// <summary>Identifies the PhysicianId entity attribute.</summary>
public const string PhysicianIdField = "PhysicianId";
/// <summary>Identifies the ProductGroupId entity attribute.</summary>
public const string ProductGroupIdField = "ProductGroupId";
#endregion
#region Relationships
[ReverseAssociation("TransfusionRecords")]
private readonly EntityHolder<Document> _document = new EntityHolder<Document>();
[ReverseAssociation("TransfusionRecords")]
private readonly EntityHolder<Physician> _physician = new EntityHolder<Physician>();
[ReverseAssociation("TransfusionRecords")]
private readonly EntityHolder<ProductGroup> _productGroup = new EntityHolder<ProductGroup>();
#endregion
#region Properties
public Document Document
{
get { return Get(_document); }
set { Set(_document, value); }
}
public Physician Physician
{
get { return Get(_physician); }
set { Set(_physician, value); }
}
public ProductGroup ProductGroup
{
get { return Get(_productGroup); }
set { Set(_productGroup, value); }
}
public string DonorNumber
{
get { return Get(ref _donorNumber, "DonorNumber"); }
set { Set(ref _donorNumber, value, "DonorNumber"); }
}
public string ProductType
{
get { return Get(ref _productType, "ProductType"); }
set { Set(ref _productType, value, "ProductType"); }
}
public System.Nullable<bool> Irradiated
{
get { return Get(ref _irradiated, "Irradiated"); }
set { Set(ref _irradiated, value, "Irradiated"); }
}
public System.Nullable<bool> Uncrossmatched
{
get { return Get(ref _uncrossmatched, "Uncrossmatched"); }
set { Set(ref _uncrossmatched, value, "Uncrossmatched"); }
}
public System.Nullable<System.DateTime> ReleasedOn
{
get { return Get(ref _releasedOn, "ReleasedOn"); }
set { Set(ref _releasedOn, value, "ReleasedOn"); }
}
/// <summary>Gets or sets the ID for the <see cref="Document" /> property.</summary>
public int DocumentId
{
get { return Get(ref _documentId, "DocumentId"); }
set { Set(ref _documentId, value, "DocumentId"); }
}
/// <summary>Gets or sets the ID for the <see cref="Physician" /> property.</summary>
public System.Nullable<int> PhysicianId
{
get { return Get(ref _physicianId, "PhysicianId"); }
set { Set(ref _physicianId, value, "PhysicianId"); }
}
/// <summary>Gets or sets the ID for the <see cref="ProductGroup" /> property.</summary>
public int ProductGroupId
{
get { return Get(ref _productGroupId, "ProductGroupId"); }
set { Set(ref _productGroupId, value, "ProductGroupId"); }
}
/// <summary>Gets the time the entity was created</summary>
public System.DateTime CreatedOn
{
get { return _createdOn; }
}
/// <summary>Gets the time the entity was last updated</summary>
public System.DateTime UpdatedOn
{
get { return _updatedOn; }
}
/// <summary>Gets the time the entity was soft-deleted</summary>
public System.Nullable<System.DateTime> DeletedOn
{
get { return _deletedOn; }
}
#endregion
}
The stack trace is:
[LightSpeedException: Circular associations are not supported by LightSpeed: [TransfusionRecord.Document and Document.Document]]
..(TypeModel , LinkedList`1 ) +479
..(IEnumerable`1 ) +97
Mindscape.LightSpeed.Model.TypeModel.() +76
Mindscape.LightSpeed.Model.TypeModel.GetTypeModel(Type ) +822
..(Query , IList ) +96
Mindscape.LightSpeed.UnitOfWork.Find(Query query, IList results) +148
Mindscape.LightSpeed.UnitOfWorkBase.Find(Query query) +102
Mindscape.LightSpeed.Linq.Plan.SingleQueryPlan.ExecuteImmediate(IUnitOfWork unitOfWork, Type returnType) +468
Mindscape.LightSpeed.Linq.LinqQueryProvider.Execute(Expression expression) +247
Mindscape.LightSpeed.Linq.LinqQuery`1.GetEnumerator() +38
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +382
System.Linq.Enumerable.ToList(IEnumerable`1 source) +80
ColumbiaHealthcare.UtilizationReview.Infrastructure.Web.Controllers.RegistrationController.TransfusionRecordsList() in D:\Development\Projects\ColumbiaHealthcare\src\ColumbiaHealthcare.UtilizationReview\Infrastructure\Web\Controllers\RegistrationController.cs:187
lambda_method(Closure , ControllerBase , Object[] ) +79
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +258
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +39
System.Web.Mvc.<>c__DisplayClassd.<InvokeActionMethodWithFilters>b__a() +125
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +640
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +312
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +709
System.Web.Mvc.Controller.ExecuteCore() +162
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__4() +58
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +20
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +453
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +371