I have tried many different mapping configurations but continue to generate exceptions or create / update the wrong record.
Exceptions
- Cannot insert explicit value for identity column in table
- Cannot use identity column key generation with union-subclass mapping
- null id generated for class XXX
When I have a map that does save I have the following problem
I have successfully inserted and updated records in the database but these records do not have the appropriate id. They all have 0 for an id and as such only update the same record over and over.
Problem I am trying to solve
I am attempting to SubclassMap the interface IRequest. This interface is used as a property on a separate class AbstractWorkflowRequestInformation. When saving the parent class I want to save the referenced IRequest in the appropriate sub-class table. This is my current mapping which generates the exception Cannot insert explicit value for identity column in table. I am sure I have something tweaked in the way I am mapping the relationship between these two classes. What am I doing wrong? My maps are below.
IRequestMap
public class IRequestMap : ClassMap<IRequest>
{
public IRequestMap()
{
Id(x => x.WorkflowRequestInformation)
.GeneratedBy.Foreign("AbstractWorkflowRequestInformation");
UseUnionSubclassForInheritanceMapping();
}
}
public class PlanRequestMap : SubclassMap<PlanRequest>
{
public PlanRequestMap()
{
Table("plan_request");
// specific to PlanRequest property mappings and references
}
}
public class BnjRequestMap : SubclassMap<BnjRequest>
{
public BnjRequestMap()
{
Table("scratchdb.guest.bnj_request");
// specific to BnjRequest property mappings and references
}
}
AbstractWorkflowRequestInformationMap
public class AbstractWorkflowRequestInformationMap :
ClassMap<AbstractWorkflowRequestInformation>
{
public AbstractWorkflowRequestInformationMap()
{
Table("workflow_request_information");
Id(x => x.Id)
.Column("workflow_request_information_id")
.GeneratedBy.Identity();
// more property mappings and references
References(x => x.SuperDuperRequest, "workflow_request_information_id")
.Class<IRequest>().Unique().Cascade.All();
// more property mappings and references
}
}
The issue here was that my IRequest map was missing a constraining reference back to AbstractWorkflowRequestInformation. So by adding a WorkflowRequestInformation property and the constraint everything now works as I intended.