MSSQL & Oracle - Identifier mapping - native for identities and sequences

Jun 19, 2009 at 7:12 AM

Hi folks,

Just one stuff concerning this project: congratulations! it's easy to use and very intuitive :-) :-)

I'm getting one issue when using IDs in SQLServer and same code for sequences in Oracle . I send you holding C# class, mapping xml file and table definition:

C# Class:

    public class AnomaliaDestinatario
        private Int32 _Id;
        private string _CodigoUsuario = "";

        public Int32 Id
            get { return _Id; }
            set { _Id = value; }

        public string CodigoUsuario
            get { return _CodigoUsuario; }
            set { _CodigoUsuario = value; }

Mapping xml file:

<Entity type="Evalos:Logica:Anomalias:AnomaliaDestinatario" table="ANOMALIASCONFIGEMPLEADOS">
    <Id field="ACE_ID">
        <Generator name="native">
        <Property name="dbType">Int32</Property>
        <Property name="seed">1</Property>
        <Property name="increment">1</Property>
    <Attribute name="Id" field="ACE_ID" db-type="Int32" not-null="true" />
    <Attribute name="CodigoUsuario" field="ACE_CODUSUARIO" db-type="String" size="50" not-null="false" />

Table definition:

    [ACE_ID] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
    [ACE_CODUSUARIO] [nvarchar](50) NULL,

The problem is when I'm trying to add a new row with this code in MSSQL, I'm getting "I can't add a value to an identiy column" meesage. And when I'm trying to add a new row with this code in ORACLE, I'm getting "ORA-01400 you can add a null value to ACE_ID" message.

Which is the best practise for using Identities and sequences?


Josep Balague



Jul 9, 2009 at 10:34 AM

I think you should try to remove this line from your mapping file:

<Attribute name="Id" field="ACE_ID" db-type="Int32" not-null="true" />

As you have mapped the Identifier of you class with the ACE_ID field, and that by default, any Id or CLASSNAMEId property will be used as the mapped Id, you don't need this line, and moreover will break this behavior.