日期上的ServiceStack ORMLite SqlServer发布错误

时间:2014-06-11 04:27:28

标签: sql-server servicestack ormlite-servicestack

ServiceStack的新手,并尝试了解JSon客户端和SqlServer数据库的一些示例。我有像这样的DTO对象:

    public class InspectionResults : IHasIntId
{
    [AutoIncrement]
    [PrimaryKey]
    [Alias("OID")]
    public int Id { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public string Instructions { get; set; }
    public string Frequency { get; set; }
    public Boolean Active { get; set; }
    public DateTime ChangedDate { get; set; }
    public DateTime CreatedDate { get; set; }
    public int LocationID { get; set; }
    public DateTime AssignedDate { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public int InspectorID { get; set; }
    public string Notes { get; set; }
}

当我填充属性并尝试发布我收到Sqlerrors with Overflow on Date。这是我的Populate和Post。属性中的所有数据都显示正常。

 var client = new JsonServiceClient("http://localhost:50238/InspectionResults?");
        var res = client.Post( new InspectionResults
        {
            Active = inspection.Active,
            AssignedDate = DateTime.Now.AddHours(-3),
            ChangedDate = DateTime.Now,
            CreatedDate = DateTime.Now,
            Description = inspection.Description,
            EndDate = DateTime.Now,
            Frequency = inspection.Frequency,
            InspectorID = 2,
            Instructions = inspection.Instructions,
            LocationID = inspection.LocationID,
            Notes = "These are my dummy notes for the whole Inspection",
            StartDate = DateTime.Now.AddMinutes(-55),
            Title = inspection.Title
         });

以下是响应主体

{"ResponseStatus":{"ErrorCode":"SqlTypeException","Message":"SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.","StackTrace":"[InspectionResultsRequest: 6/11/2014 4:22:48 AM]:\n[REQUEST: {Id:0,Title:Clean Bathrooms,Description:First Floor Mens & Ladies Rooms,Instructions:Clean them Good,Frequency:\"\",Active:True,ChangedDate:2014-06-11T00:22:47.9550000-04:00,CreatedDate:2014-06-11T00:22:47.9550000-04:00,LocationID:1,AssignedDate:2014-06-10T21:22:47.9550000-04:00,StartDate:2014-06-10T23:27:47.9550000-04:00,EndDate:2014-06-11T00:22:47.9550000-04:00,InspectorID:2,Notes:These are my dummy notes for the whole Inspection}]\nSystem.Data.SqlTypes.SqlTypeException: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.\r\n   at System.Data.SqlClient.TdsParser.TdsExecuteRPC(_SqlRPC[] rpcArray, Int32 timeout, Boolean inSchema, SqlNotificationRequest notificationRequest, TdsParserStateObject stateObj, Boolean isCommandProc, Boolean sync, TaskCompletionSource`1 completion, Int32 startRpc, Int32 startParam)\r\n   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)\r\n   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)\r\n   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)\r\n   at System.Data.SqlClient.SqlCommand.ExecuteScalar()\r\n   at ServiceStack.OrmLite.OrmLiteReadExtensions.LongScalar(IDbCommand dbCmd)\r\n   at ServiceStack.OrmLite.OrmLiteResultsFilterExtensions.ExecLongScalar(IDbCommand dbCmd, String sql)\r\n   at ServiceStack.OrmLite.OrmLiteDialectProviderBase`1.InsertAndGetLastInsertId[T](IDbCommand dbCmd)\r\n   at ServiceStack.OrmLite.OrmLiteWriteExtensions.Insert[T](IDbCommand dbCmd, T obj, Boolean selectIdentity)\r\n   at ServiceStack.OrmLite.OrmLiteWriteConnectionExtensions.<>c__DisplayClass22`1.<Insert>b__21(IDbCommand dbCmd)\r\n   at ServiceStack.OrmLite.OrmLiteExecFilter.Exec[T](IDbConnection dbConn, Func`2 filter)\r\n   at ServiceStack.OrmLite.ReadConnectionExtensions.Exec[T](IDbConnection dbConn, Func`2 filter)\r\n   at ServiceStack.OrmLite.OrmLiteWriteConnectionExtensions.Insert[T](IDbConnection dbConn, T obj, Boolean selectIdentity)\r\n   at InspectorService.DataWorker.InspectionResultsDataWorker.AddInspectionResults(InspectionResults i) in c:\\Temp\\InspectorService\\InspectorService\\DataWorker\\InspectionResultsDataWorker.cs:line 27\r\n   at InspectorService.InspectionResultsService.Post(InspectionResultsRequest request) in c:\\Temp\\InspectorService\\InspectorService\\Services\\InspectionResultsService.cs:line 38","Errors":[]}}

感谢您的任何指导。

添加了DataWorkerClass:

public class InspectionResultsDataWorker
 {
    /// <summary>
    /// A helper class that uses the ServiceStack.OrmLite Object Relational Mapper
    /// to support CRUD operations on the InspectionResults table.
    /// </summary> 
    // The IDbConnection passed in from the IOC container on the service
    System.Data.IDbConnection _dbConnection;

    // Store the database connection passed in
    public InspectionResultsDataWorker(System.Data.IDbConnection dbConnection)
    {
        _dbConnection = dbConnection;
    }

    // Inserts a new row into the InspectionResults table
    public int AddInspectionResults(InspectionResults i)
    {
        return (int)_dbConnection.Insert<InspectionResults>(i, selectIdentity: true);
    }

    // Return a list of InspectionResultss from our DB
    // (this is the equivilent of “SELECT * FROM InspectionResults”)
    public List<InspectionResults> GetInspectionResultsList()
    {
        var inspectionResults = _dbConnection.Select<InspectionResults>();
        var inspectionResultsItems = _dbConnection.Select<InspectionResultsItem>();
        List<InspectionResults> returnList = new List<InspectionResults>();

        foreach (InspectionResults ins in inspectionResults)
        {
            List<InspectionResultsItem> inspItemList = _dbConnection.Select<InspectionResultsItem>(q => q.InspectionResultsID == ins.Id);
    //        ins.InspectionResultsItems = inspItemList;
            returnList.Add(ins);

        }
        return returnList;
    }

    // Return a single InspectionResults given its ID
    public InspectionResults GetInspectionResultsByID(int id)
    {
        return _dbConnection.SingleById<InspectionResults>(id);
    }

    // Updates a row in the InspectionResults table. Note that this call updates
    // all fields, in order to update only certain fields using OrmLite,
    // use an anonymous type like the below line, which would only
    // update the Title and Description fields:
    // _dbConnection.Update(new { Title = “Inspect Tractors”, Description = “Check all Aspects of Tractor Operation” });
    public InspectionResults UpdateInspectionResults(InspectionResults i)
    {
        _dbConnection.Update<InspectionResults>(i);
        return i;
    }

    // Deletes a row from the InspectionResults table
    public int DeleteInspectionResultsByID(int id)
    {
        return _dbConnection.Delete(id);
    }

}

添加了Service Post和Add方法:          //创建一个新的InspectionResults          public int Post(InspectionResultsRequest request)          {             var p = new InspectionResults()             {

        };

        InspectionResultsDataWorker pdp = new InspectionResultsDataWorker(Db);
        return pdp.AddInspectionResults(p);
    }

 // Inserts a new row into the InspectionResults table
    public int AddInspectionResults(InspectionResults i)
    {
        return (int)_dbConnection.Insert<InspectionResults>(i, selectIdentity: true);
    }

0 个答案:

没有答案