Menu

SQL Query problem

Phil
2006-08-16
2013-04-22
  • Phil

    Phil - 2006-08-16

    Hi,
    I am evaluating DomainObjects for a project and I have a problem when creating and object.

    The insert query is :
    INSERT INTO DEMANDE (PATIENTID, SAMPLEID) VALUES (@DEMANDE01_PATIENTID_0, @DEMANDE01_SAMPLEID_1);select SCOPE_IDENTITY();0, 9'

    and the insert does not contain the primary key entry : column ID.
    the table script is :
    CREATE TABLE [dbo].[DEMANDE](
        [ID] [int] NOT NULL,
        [PATIENTID] [int] NULL,
        [SAMPLEID] [int] NOT NULL,
    CONSTRAINT [PK_DEMANDEIDINDEX] PRIMARY KEY CLUSTERED
    (
        [ID] ASC
    )WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
    ) ON [PRIMARY]

    the C# source code of demande is
        [Serializable]
        public class Demande : EditableObject {

            public bool state {
                get { return false; }
            }

            private int _patientid;

            [Mutator]
            public int patientId {
                get { return _patientid;}
                set { _patientid = value; }
            }

            private int _sampleId;

            [Mutator]
            public int sampleId {
                get { return this._sampleId; }
                set { this._sampleId = value;}
            }

            protected Demande(string primaryKey, int patientid, int sampleId)
                : base(primaryKey) {
                this._patientid = patientId;
                this._sampleId = sampleId;
            }

            public Demande(int sampleId)    {
                this._sampleId = sampleId;
            }

        }

    Do you have any idea ?

    Do you have example of how to update and delete object ?

    regards

    Philippe

     
    • Richard Beauchamp

      Hi Philippe,

      What does your ClassDescriptor for Demande look like?

      And do you want to use identity columns or set the primary key yourself?

       
    • Phil

      Phil - 2006-08-16

      I try with the tools RepositoryGen and DomainObjectGen. I get the generated files and I have the same error.
      My ClassDescriptor.xml is :
        <ClassDescriptor ClassName="com.diagast.testdbmapping.Demande" TableName="DEMANDE">
          <PrimaryKeyDescriptor IsAutoIncremented="true">
            <FieldDescriptor Id="DEMANDE1" FieldName="_primaryKey" IsDeclaredInBaseClass="true" DbType="Int32" ColumnName="ID" Precision="10" />
          </PrimaryKeyDescriptor>
          <FieldDescriptor Id="DEMANDE2" FieldName="_patientid" PropertyName="Patientid" DbType="Int32" IsNullable="true" ColumnName="PATIENTID" Precision="10" />
          <FieldDescriptor Id="DEMANDE3" FieldName="_sampleid" PropertyName="Sampleid" DbType="Int32" ColumnName="SAMPLEID" Precision="10" />
        </ClassDescriptor>

      TO set the primary key I onmy use the CONSTRAINT [PK_DEMANDEIDINDEX] PRIMARY KEY . Is it enougth ? If not what I have to do?

      regards

      Philippe Delrieu

       
    • Phil

      Phil - 2006-08-16

      I try with the identity option and it work.
      I would like to use string as primary key and not int. Does I have to do special things ? I think I have to provide the primary key at creation. Is it enougth ?

      I try to modify the created demande :
      mydemande.PatienID = 12;
      The db isn't updated. What I have to do to commit object change or delete ?

      Regards

      Philippe Delrieu

       
      • Richard Beauchamp

        Hi Philippe,

        If you want to set the primary key yourself, then you need to set IsAutoIncremented="false" in your PrimaryKeyDescriptor.

        And if you want to use a string for your primary key, then you need to set DbType="AnsiString" in your PrimaryKey field descriptor.

        Regards,

        Richard

         
    • Phil

      Phil - 2006-08-17

      Hi RIchard,
      thank you for your reply.
      Do you know where I can find example of object modification and deletion.

      regards

      Philippe Delrieu

       
      • Richard Beauchamp

        The DomainObjects unit tests are an excellent place to find usage examples. Setting breakpoints in the unit tests and following the flow of the tests is a good way to understand how DomainObjects works.

        For example, see the unit test TransactionBoundaryTests.EditSameInstanceAcrossTransactions(). This unit test creates and updates objects, while the test class SetUp() method (line 46) deletes objects.

        To run and debug a unit test:

        1. Set the Visual Studio DomainObjects.Test.dll project as the startup project.

        2. Go to the DomainObjects.Test.dll project properties and set the Debug Start Action to 'Start External Program' with a value of '<YourDirectoryPathToDomainObjects>\DomainObjects\bin\nunit\nunit-gui.exe'.

        3. Start Visual Studio debugging

        4. From within NUnit, open the DLL <YourDirectoryPathToDomainObjects>\build\DomainObjects\Debug\Test\DomainObjects.Test.dll and run the unit test.

        Let me know how it goes.

        Best regards,

        Richard

         
        • Richard Beauchamp

          Actually, there is one more step to run the unit tests: you need to setup the unit test database.

          From a command window, go to the DomainObjects directory, and execute the command line:

          build refreshdb

          This will create a DomainObjects unit test SQL Server database on your (local) instance.

          Let me know if you have any issues. (We might be able to make this setup process easier for initial users...)

          Richard

           

Log in to post a comment.