ServiceStack ORMLite SqlServer在日期上发布错误

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。 这是我的填充和发布。 属性中的所有数据都显示正常。

 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
         });

以下是Response Body

{"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.rn   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)rn   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)rn   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)rn   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)rn   at System.Data.SqlClient.SqlCommand.ExecuteScalar()rn   at ServiceStack.OrmLite.OrmLiteReadExtensions.LongScalar(IDbCommand dbCmd)rn   at ServiceStack.OrmLite.OrmLiteResultsFilterExtensions.ExecLongScalar(IDbCommand dbCmd, String sql)rn   at ServiceStack.OrmLite.OrmLiteDialectProviderBase`1.InsertAndGetLastInsertId[T](IDbCommand dbCmd)rn   at ServiceStack.OrmLite.OrmLiteWriteExtensions.Insert[T](IDbCommand dbCmd, T obj, Boolean selectIdentity)rn   at ServiceStack.OrmLite.OrmLiteWriteConnectionExtensions.<>c__DisplayClass22`1.<Insert>b__21(IDbCommand dbCmd)rn   at ServiceStack.OrmLite.OrmLiteExecFilter.Exec[T](IDbConnection dbConn, Func`2 filter)rn   at ServiceStack.OrmLite.ReadConnectionExtensions.Exec[T](IDbConnection dbConn, Func`2 filter)rn   at ServiceStack.OrmLite.OrmLiteWriteConnectionExtensions.Insert[T](IDbConnection dbConn, T obj, Boolean selectIdentity)rn   at InspectorService.DataWorker.InspectionResultsDataWorker.AddInspectionResults(InspectionResults i) in c:TempInspectorServiceInspectorServiceDataWorkerInspectionResultsDataWorker.cs:line 27rn   at InspectorService.InspectionResultsService.Post(InspectionResultsRequest request) in c:TempInspectorServiceInspectorServiceServicesInspectionResultsService.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请求){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);
    }
链接地址: http://www.djcxy.com/p/68711.html

上一篇: ServiceStack ORMLite SqlServer Post error on Dates

下一篇: ServiceStack post request with dynamic or DynamicTableEntity object