0

So I am new to NPOCO and virtually have no experience with Oracle! But I am tasked with crating a method that writes some data to a table! here is my situation. I created my Model

[TableName("API_ANALYTICAL_DATA")]
[PrimaryKey("GID")]
public class ApiAnalyticalData 
{
    public ApiAnalyticalData() { }

    [Ignore]
    public string GID { get; set; }

    [Column("RESOURCE_UID"), ColumnType(typeof(OracleNumber))]
    public decimal ResourceID { get; set; }

    [Column("REQUEST_TIME"), ColumnType(typeof(OracleDateTime))]
    public string RequestDateTime { get; set; }

    [Column("COMPLETION_TIME"), ColumnType(typeof(OracleDateTime))]
    public string CompletionTime { get; set; }

    [Column("STATUS_CODE"), ColumnType(typeof(OracleNumber))]
    public decimal StatusCode { get; set; }

    [Column("STATUS_MESSAGE"), ColumnType(typeof(OracleString))]
    public string StatusMessage {get;set;}

    [Column("REMOTE_ADDRESS"), ColumnType(typeof(OracleString))]
    public string RemoteAddress { get; set; }

    [Column("REQUEST_URL"), ColumnType(typeof(OracleString))]
    public string RequestUrl { get; set; }

    [Column("REQUEST_METHOD"), ColumnType(typeof(OracleString))]
    public string RequestMethod { get; set; }

    [Column("RESPONSE_HEADERS"), ColumnType(typeof(OracleString))]
    public string ResponseHeaders { get; set; }

    [Column("REQUEST_HEADERS"), ColumnType(typeof(OracleLob))]
    public string RequestHeaders { get; set; }

    [Column("REQUESTOR_UUPIC"), ColumnType(typeof(OracleString))]
    public string RequstorID { get; set; }

    [Column("IS_RESOURCE_UID_FK_VALID"), ColumnType(typeof(OracleString))]
    public string IsResourceIdValid { get; set; }

    [Column("ERR_MESSAGE"), ColumnType(typeof(OracleLob))]
    public string ErrorMessage { get; set; }
}

I also have a data mapper setup

public class DataMappings : Mappings 
{
    public DataMappings()
    {
        For<ApiAnalyticalData>()
            .TableName("API_ANALYTICAL_DATA")
            .PrimaryKey("GID")
            .Columns(d => {
                d.Column(c => c.GID).Ignore();
                d.Column(c => c.ResourceID).WithName("RESOURCE_UID").WithDbType(typeof(OracleNumber));
                d.Column(c => c.RequestDateTime).WithName("REQUEST_TIME").WithDbType(typeof(OracleDateTime));
                d.Column(c => c.CompletionTime).WithName("COMPLETION_TIME").WithDbType(typeof(OracleDateTime));
                d.Column(c => c.StatusCode).WithName("STATUS_CODE").WithDbType(typeof(OracleNumber));
                d.Column(c => c.StatusMessage).WithName("STATUS_MESSAGE").WithDbType(typeof(OracleString));
                d.Column(c => c.RemoteAddress).WithName("REMOTE_ADDRESS").WithDbType(typeof(OracleString));
                d.Column(c => c.RequestUrl).WithName("REQUEST_URL").WithDbType(typeof(OracleString));
                d.Column(c => c.RequestMethod).WithName("REQUEST_METHOD").WithDbType(typeof(OracleString));
                d.Column(c => c.ResponseHeaders).WithName("RESPONSE_HEADERS").WithDbType(typeof(OracleString));
                d.Column(c => c.RequestHeaders).WithName("REQUEST_HEADERS").WithDbType(typeof(OracleLob));
                d.Column(c => c.RequstorID).WithName("REQUESTOR_UUPIC").WithDbType(typeof(OracleString));
                d.Column(c => c.IsResourceIdValid).WithName("IS_RESOURCE_UID_FK_VALID").WithDbType(typeof(OracleString));
                d.Column(c => c.ErrorMessage).WithName("ERR_MESSAGE").WithDbType(typeof(OracleLob));
        });
    }
}

And my DB factory

 public static class DBFactory
{
    public static DatabaseFactory Databasefactory { get; set; }
    public static void Setup(string DBConnnectionString)
    {
        var dbConfig = FluentMappingConfiguration.Configure(new DataMappings()); 
        Databasefactory = DatabaseFactory.Config( x => {
            x.UsingDatabase( () => new Database(DBConnnectionString)); 
            x.WithFluentConfig(dbConfig); 
            x.WithMapper( new Mapper()); 
        });
    }
}

So I call the this setup method on application start up

protected void Application_Start()
{
      DBFactory.Setup("uptmstora");
}

And this is the method that will insert the data

public T PostData<T>(T model)
        {  

            dbContext.Insert<T>(model);
            return model;
        }

and it is called like so

 var DataModel = JsonConvert.DeserializeObject<ApiAnalyticalData>(json);
 PostData(DataModel)

All this works fine until I get the dbContext.Insert(model); I get an exception, Oracal.DataAccess.Client.OracleException with exception message "ORA-00932: inconsistent datatypes: expected NUMBER got BINARY"

I have been over and over the data, and everything looks good, not sure what else to do with it at this point. So I guess the question is, does anyone have any kind of experience with NPOCO and saving data into oracle, I mean I tried with and with out the Mapper and I get the same error. Here is a sample of the JSON that gets de-Serialized into my model :

{
  "ResourceID": 54469,
  "RequestDateTime": "31-AUG-15",
  "CompletionTime": "1-SEP-15",
  "StatusCode": 200,
  "ErrorMessage": "",
  "StatusMessage": "Success",
  "RemoteAddress": "192.1.1.2",
  "RequestUrl": "http://www.somewebservice.com/",
  "RequestMethod": "SomeMethod",
  "ResponseHeaders": "Success 200",
  "RequestHeaders": "GET",
  "RequstorID": "1000",
  "IsResourceIdValid": "Y"
}

EDIT: Here is the table as requested

enter image description here

ksliman
  • 595
  • 1
  • 8
  • 21
  • Please edit your question and add the database definition of the table in question. Thanks. – Bob Jarvis - Слава Україні Sep 09 '15 at 21:12
  • Why are you ignoring primary key mapping? Try removing the `[Ignore]` directive on the primary key and under mappings use `.PrimaryKey("GID", false)` to tell NPoco you will be assigning the id and insert using `dbContext.Insert(model);` – kagundajm Sep 10 '15 at 12:35
  • Thank you, I tried your changes, and I still get the same exception. I guess I was under the impression that I must ignore the primary key because it is auto generated! So wouldn't PrimaryKey("GID", false) mean I have to provide the value for the PrimaryKey – ksliman Sep 10 '15 at 13:15
  • Ok so maybe its the oracle side of things, but I just took another look and and If try CalMeKags suggestions, but I supply my own guid it works. The problem there is the GUID is supposed to be supplied by Oracle server not the Client application! As I said earlier, this is my first experience with oracle, but from looking at the PL Sql it looks like this for the GID field "GID" RAW(16) DEFAULT sys_guid() NOT NULL ENABLE Which tells me that if one is not supplied the system will supply one right ? – ksliman Sep 10 '15 at 13:44
  • I don't have Oracle on my box but I think you would need to [code a trigger](http://stackoverflow.com/questions/2284738/is-there-a-way-to-create-an-auto-incrementing-guid-primary-key-in-an-oracle-data#comments-2284826). You can refer to [this answer](http://stackoverflow.com/questions/11296361/how-to-create-id-with-auto-increment-on-oracle#answer-11296469) on how to generate a trigger. – kagundajm Sep 10 '15 at 17:28

0 Answers0