Document class is class that represents one document and DocumentLine is class that represents one document part (line) i.e. master - details relationship.
I am getting the following exception:
INSERT statement conflicted with COLUMN FOREIGN KEY constraint 'FK_document_lines_to_document'. The conflict occurred in database 'mydatabase', table 'document', column 'id'.
The reason is that DocumentLine.DocumentID is not initialized with the newly created (initialized) Document.ID value.
Document.ID value is initialized when document object is inserted to the database but DocumentLine object is not automatically updated (DocumentID property), so insert fails because constraint database check (there is no document with id = 0 in the document table)
Classes are made like was suggested in the AtomsFramework 'Basic Tutorial'.
My code for creation and save goes:
Document d = new Document();
DocumentLine dl = new DocumentLine();
d.Items.Add ( dl );
d.Save(); //exception thrown (Document is saved ok but DocumentLines (CPersistentCollection) not
At the time of save both objects are valid and dirty.
How to solve this problem. Thanks in advance,
Sanjin
Here is the database.
CREATE TABLE [dbo].[document] (
[id] [int] NOT NULL IDENTITY (1, 1)
) ON [PRIMARY]
CREATE TABLE [dbo].[document_lines] (
[idrec] [int] NOT NULL IDENTITY (1, 1),
[document_id] [int] NOT NULL
) ON [PRIMARY]
ALTER TABLE dbo.document_lines WITH CHECK ADD
CONSTRAINT FK_document_lines_to_document
FOREIGN KEY (document_id)
REFERENCES dbo.document (id)
GO
I have problem saving document lines.
Document class is class that represents one document and DocumentLine is class that represents one document part (line) i.e. master - details relationship.
I am getting the following exception:
INSERT statement conflicted with COLUMN FOREIGN KEY constraint 'FK_document_lines_to_document'. The conflict occurred in database 'mydatabase', table 'document', column 'id'.
The reason is that DocumentLine.DocumentID is not initialized with the newly created (initialized) Document.ID value.
Document.ID value is initialized when document object is inserted to the database but DocumentLine object is not automatically updated (DocumentID property), so insert fails because constraint database check (there is no document with id = 0 in the document table)
Classes are made like was suggested in the AtomsFramework 'Basic Tutorial'.
My code for creation and save goes:
Document d = new Document();
DocumentLine dl = new DocumentLine();
d.Items.Add ( dl );
d.Save(); //exception thrown (Document is saved ok but DocumentLines (CPersistentCollection) not
At the time of save both objects are valid and dirty.
How to solve this problem. Thanks in advance,
Sanjin
Here is the database.
CREATE TABLE [dbo].[document] (
[id] [int] NOT NULL IDENTITY (1, 1)
) ON [PRIMARY]
CREATE TABLE [dbo].[document_lines] (
[idrec] [int] NOT NULL IDENTITY (1, 1),
[document_id] [int] NOT NULL
) ON [PRIMARY]
ALTER TABLE dbo.document_lines WITH CHECK ADD
CONSTRAINT FK_document_lines_to_document
FOREIGN KEY (document_id)
REFERENCES dbo.document (id)
GO
Xml mapping:
<class name="Document" table="document" database="MyDatabase">
<attribute name="ID" column="id" key="primary" identity="true"/>
<attribute name="Items"/> <!-- DocumentLines collection -->
</class>
<class name="DocumentLine" table="document_lines" database="MyDatabase">
<attribute name="ID" column="id" key="primary" identity="true"/>
<attribute name="DocumentID" column=" document_id" find="true" proxy="true"/>
<attribute name="Doc"/> <!-- Document object -->
</class>
<association fromClass="Document"
toClass="DocumentLines"
cardinality="oneToMany"
target="Items"
retrieveAutomatic="true"
deleteAutomatic="true"
saveAutomatic="true"
inverse="false">
<entry fromAttribute="ID" toAttribute="DocumentID"/>
</association>
<association fromClass="DocumentLine"
toClass="Document"
cardinality="oneToOne"
target="Doc"
retrieveAutomatic="false"
deleteAutomatic="false"
saveAutomatic="false"
inverse="false">
<entry fromAttribute="DocumentID" toAttribute="ID"/>
</association>
This is a bit of a problem as the framework doesn't currently respect foreign keys.
There are plans to address this, but time is rather limited at the moment and I won't be able to tackle it properly any time soon.
You could remove the FK constraints and it will work properly, but that might not be the best option for you....
- Richard