1

我在我的 SPA 应用程序中使用 Breeze.js 作为数据服务,但我想使用存储过程来删除我在 SQL 数据库中的一些实体。

我设法让客户端调用数据库中的存储过程,但我仍然有一些问题。

在 Breeze 控制器中调用存储过程的方法如下所示:

[HttpGet]
    [ActionName("deletecustomer")]
    public object DeleteCustomer([FromUri] string customerName)
    {
        string query = "sp_delete_customer @CustomerName";
        SqlParameter CustomerName = new SqlParameter("@CustomerName", customerName);
        var result = _dataContext.Context.Database.SqlQuery<Customer>(query, CustomerName);
        return result;
    }

我是从微风经理那里打电话的:

 var deleteCustomer = function (customerName) {
        var query = EntityQuery.from('DeleteCustomer')
               .withParameters({ customerName: customerName });

        return manager.executeQuery(query)
            .then(querySucceeded)
            .fail(queryFailed);
}

问题是我仍然收到一个Internal Server Error

{"$id":"1","$type":"System.Web.Http.HttpError,System.Web.Http",
"Message":"Anerrorhasoccurred.",
"ExceptionMessage":"The'ObjectContent`1'typefailedtoserializetheresponsebodyforcontenttype'application/json;charset=utf-8'.",
"ExceptionType":"System.InvalidOperationException","StackTrace":null,
"ExceptionMessage":"Thedatareaderisincompatiblewiththespecified'NotosDB.Data.Customer'.Amemberofthetype,'CustomerID',doesnothaveacorrespondingcolumninthedatareaderwiththesamename.","
....

编辑:我正在使用 Code First 开发。

我知道它与 JSON 序列化/反序列化有关,但究竟是什么问题?

4

1 回答 1

0

因为 Breeze.js 期望结果是Customer类型的实体,所以我放弃了管理器和 EntityQuery 的使用,转而支持微风的 AJAX 实现。

现在看起来像这样:

控制器:

[HttpPost]
    [ActionName("deletecustomer")]
    public object DeleteCustomer(HttpRequestMessage request)
    {
        var data = request.Content.ReadAsFormDataAsync().Result;
        var customerName = data["customerName"];
        string query = "sp_delete_customer @CustomerName";
        SqlParameter CustomerName = new SqlParameter("@CustomerName", customerName);
        var result = _dataContext.ObjectContext.ExecuteStoreCommand(query, CustomerName);
        return result.ToString();
    }

调用函数:

    var ajaxImpl = breeze.config.getAdapterInstance("ajax");
    var deleteCustomer = function (callback, custName) {
        function querySucceeded(data) {
            log('Customer ' + custName + ' deleted in '+ data +' ms!',
                data, true);
            callback();
            return true;
        }
        return ajaxImpl.ajax({
            type: "POST",
            url: "/breeze/DeleteCustomer",
            data: { customerName: custName },
            success: querySucceeded,
            error: queryFailed
        });
    };

现在我可以调用存储过程并返回结果。

于 2013-11-07T14:34:01.587 回答