Breeze Controller没有返回正确的响应

时间:2013-02-27 13:40:12

标签: breeze

我有一个简单的模型,它是Entity Framework 5 Code First,ActiveEntity是一个带有int Id属性和bool IsActive字段的抽象类。

public class License:ActiveEntity
{
    public string LicenseName { get; set; }

    public LicenseType LicenseType { get; set; }

    public State State { get; set; }

    public DateTime DateIssued { get; set; }

    public int ValidFor { get; set; }


}

public class LicenseType:ActiveEntity 
{
    [StringLength(100),Required]
    public string Description { get; set; }
}
public class State:ActiveEntity
{
    [StringLength(2)]
    [Required]
    public string Name { get; set; }

    [Display(Name = "Long Name")]
    [Required, StringLength(25)]
    public string LongName { get; set; }
}

Breeze在LicenseController上调用GetLicenses:

[BreezeController]
public class LicenseController : ApiController
{
    private readonly EFContextProvider<LicensingContext> db = new EFContextProvider<LicensingContext>();

    [HttpGet]
    public string Metadata()
    {
        return db.Metadata();
    }

    [HttpPost]
    public SaveResult SaveChanges(JObject saveBundle)
    {
        return db.SaveChanges(saveBundle);
    }


    [HttpGet]
    public IQueryable<License> GetLicenses()
    {
        //for debugging purposes
        var retVal = db.Context.Licenses
            .Include(l => l.State)
            .Include(l=>l.LicenseType);
        return retVal;

    }


}

db上下文返回相应的数据,但它不会出现在响应中。 我没有足够的声誉点来发布图片,但许可证类型和状态都在上下文的响应中。

但是,控制器的响应不包含前三个对象的licensetype对象。 [{&#34; $ id&#34;:&#34; 1&#34;,&#34; $ type&#34;:&#34; Volt.Telecom.Licensing.Models.License,Volt.Telecom.Licensing .Models&#34;,&#34; LicenseName&#34;:&#34; Low Voltage&#34;,&#34; State&#34;:{&#34; $ id&#34;:&#34; 2& #34;,&#34; $ type&#34;:&#34; Volt.Telecom.Licensing.Models.State,Volt.Telecom.Licensing.Models&#34;,&#34; Name&#34;:&# 34; FL&#34;&#34; LONGNAME&#34;:&#34;佛罗里达&#34;&#34; IsActive&#34;:假,&#34;标识&#34;:23},&# 34; DateIssued&#34;:&#34; 2012-11-18T00:00:00.000&#34;&#34; ValidFor&#34;:1095,&#34; IsActive&#34;:假,&#34 ;标识&#34;:1},{&#34; $ ID&#34;:&#34; 3&#34;&#34; $类型&#34;:&#34; Volt.Telecom.Licensing.Models .License,Volt.Telecom.Licensing.Models&#34;,&#34; LicenseName&#34;:&#34; Contractors&#34;,&#34; State&#34;:{&#34; $ ref&#34 ;:&#34; 2&#34;}&#34; DateIssued&#34;:&#34; 2012-11-18T00:00:00.000&#34;&#34; ValidFor&#34;:1095, &#34; IsActive&#34;:假,&#34;标识&#34;:2},{&#34; $ ID&#34;:&#34; 4&#34;&#34; $类型&# 34;:&#34; Volt.Telecom.Licensing.Models.License,Volt.Telecom.Licensing.Models&#34;,&#34; LicenseName& #34;:&#34;总承包商&#34;,&#34;州&#34;:{&#34; $ ref&#34;:&#34; 2&#34;},&#34; DateIssued&# 34;:&#34; 2012-11-18T00:00:00.000&#34;&#34; ValidFor&#34;:1095,&#34; IsActive&#34;:假,&#34;标识&#34 ;:3},{&#34; $ id&#34;:&#34; 5&#34;,&#34; $ type&#34;:&#34; Volt.Telecom.Licensing.Models.License,Volt .Telecom.Licensing.Models&#34;,&#34; LicenseName&#34;:&#34; Low Voltage&#34;,&#34; LicenseType&#34;:{&#34; $ id&#34;:& #34; 6&#34;,&#34; $ type&#34;:&#34; Volt.Telecom.Licensing.Models.LicenseType,Volt.Telecom.Licensing.Models&#34;,&#34; Description&#34 ;:&#34;低电压&#34;,&#34; IsActive&#34;:false,&#34; Id&#34;:1},&#34;状态&#34;:{&#34; $ id&#34;:&#34; 7&#34;,&#34; $ type&#34;:&#34; Volt.Telecom.Licensing.Models.State,Volt.Telecom.Licensing.Models&#34;,& #34;名称&#34;:&#34; CA&#34;&#34; LONGNAME&#34;:&#34;加州&#34;&#34; IsActive&#34;:假,&#34; ID&#34; 35},&#34; DateIssued&#34;:&#34; 2012-11-18T00:00:00.000&#34;&#34; ValidFor&#34;:1095,&#34; IsActive&#34;:假,&#34;标识&#34:4},{&#34; $ ID&#34;:&#34; 8&#34;&#34; $ TY pe&#34;:&#34; Volt.Telecom.Licensing.Models.License,Volt.Telecom.Licensing.Models&#34;,&#34; LicenseName&#34;:&#34; Contractors&#34;,&# 34;授权类型&#34; {&#34; $ ID&#34;:&#34; 9&#34;&#34; $类型&#34;:&#34; Volt.Telecom.Licensing.Models.LicenseType ,Volt.Telecom.Licensing.Models&#34;,&#34;描述&#34;:&#34;承包商&#34;,&#34; IsActive&#34;:false,&#34; Id&#34;: 2}&#34;状态&#34; {&#34; $ REF&#34;:&#34; 7&#34;}&#34; DateIssued&#34;:&#34; 2012-11- 18T00:00:00.000&#34;&#34; ValidFor&#34;:1095,&#34; IsActive&#34;:假,&#34;标识&#34;:5},{&#34; $ id&#34;:&#34; 10&#34;,&#34; $ type&#34;:&#34; Volt.Telecom.Licensing.Models.License,Volt.Telecom.Licensing.Models&#34;,& #34; LicenseName&#34;:&#34;总承包商&#34;,&#34; LicenseType&#34;:{&#34; $ id&#34;:&#34; 11&#34;,&#34 ; $ type&#34;:&#34; Volt.Telecom.Licensing.Models.LicenseType,Volt.Telecom.Licensing.Models&#34;,&#34; Description&#34;:&#34; General Contractors&#34; &#34; IsActive&#34;:假,&#34;标识&#34;:3},&#34;状态&#34; {&#34; $ REF&#34;:&#34; 7&# 34;}&#34;日期发出&#34;:&#34; 2012-11-18T00:00:00.000&#34;&#34; ValidFor&#34;:1095,&#34; IsActive&#34;:假,&#34;标识& #34;:6}]

这是客户端上的home.js文件。

define([&#39; services / logger&#39;],function(logger){     var system = require(&#39; durandal / system&#39;);

var serviceName = 'api/License';

// manager is the service gateway and cache holder
var manager = new breeze.EntityManager(serviceName);


var vm = {
    activate: getLicenses,
    title: 'Licenses',

    licenses: ko.observableArray(),
    includeExpired: ko.observable(false),
    save: saveChanges,
    show: ko.observable(false)
};

//vm.includeExpired.subscribe(getLicenses);

function getLicenses() {

    log("querying Licenses", null, true);

    var query = breeze.EntityQuery.from("GetLicenses");

    //if (!vm.includeExpired()) {
    //    query = query.where("DateIssued.AddDays(ValidFor*-1)" > new Date(Date.now()));
    //}

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

    // reload vm.todos with the results 
    function querySucceeded(data) {
        log("queried Licenses", null, true);

        vm.licenses(data.results);
        vm.show(true); // show the view
    }


}

function queryFailed(error) {
    log("Query failed: " + error.message, null, true);
}

function saveChanges() {
    return manager.saveChanges()
        .then(function () { log("changes saved", null, true); })
        .fail(saveFailed);
}

function saveFailed(error) {
    log("Save failed: " + error.message, null, true);
}

function log(msg, data, showToast) {
    logger.log(msg, data, system.getModuleId(vm), showToast);
}
return vm;
//#endregion

});

任何想法为什么会发生这种情况,只有前三项,任何帮助将不胜感激。我喜欢微风作为我们需要写的一些水疗中心的潜力,但这引起了我的一些担忧。

更新1 如果我在数据库中更改了LicenseType_id的顺序,则其初始顺序为123123 如果更改为312123或321123,则响应中的所有六个都是正确的 [{&#34; $ id&#34;:&#34; 1&#34;,&#34; $ type&#34;:&#34; Volt.Telecom.Licensing.Models.License,Volt.Telecom.Licensing .Models&#34;,&#34; LicenseName&#34;:&#34; Low Voltage&#34;,&#34; LicenseType&#34;:{&#34; $ id&#34;:&#34; 2& #34;,&#34; $ type&#34;:&#34; Volt.Telecom.Licensing.Models.LicenseType,Volt.Telecom.Licensing.Models&#34;,&#34; Description&#34;:&# 34;总承包商&#34;,&#34; IsActive&#34;:false,&#34; Id&#34;:3},&#34; State&#34;:{&#34; $ id&#34; :&#34; 3&#34;,&#34; $ type&#34;:&#34; Volt.Telecom.Licensing.Models.State,Volt.Telecom.Licensing.Models&#34;,&#34; Name& #34;:&#34; FL&#34;&#34; LONGNAME&#34;:&#34;佛罗里达&#34;&#34; IsActive&#34;:假,&#34;标识&#34; :23}&#34; DateIssued&#34;:&#34; 2012-11-18T00:00:00.000&#34;&#34; ValidFor&#34;:1095,&#34; IsActive&#34; :假,&#34;标识&#34;:1},{&#34; $ ID&#34;:&#34; 4&#34;&#34; $类型&#34;:&#34;伏.Telecom.Licensing.Models.License,Volt.Telecom.Licensing.Models&#34;,&#34; LicenseName&#34;:&#34; Contractors&#34;,&#34; LicenseType&#34;:{&# 34; $ ID&#34;:&#34; 5&#34;&#34; $类型&#34 ;:&#34; Volt.Telecom.Licensing.Models.LicenseType,Volt.Telecom.Licensing.Models&#34;,&#34;描述&#34;:&#34;低电压&#34;,&#34; IsActive&#34;:假,&#34;标识&#34;:1},&#34;状态&#34; {&#34; $ REF&#34;:&#34; 3&#34;},& #34; DateIssued&#34;:&#34; 2012-11-18T00:00:00.000&#34;&#34; ValidFor&#34;:1095,&#34; IsActive&#34;:假,&# 34;标识&#34;:2},{&#34; $ ID&#34;:&#34; 6&#34;&#34; $类型&#34;:&#34; Volt.Telecom.Licensing。 Models.License,Volt.Telecom.Licensing.Models&#34;,&#34; LicenseName&#34;:&#34; General Contractors&#34;,&#34; LicenseType&#34;:{&#34; $ id& #34;:&#34; 7&#34;,&#34; $ type&#34;:&#34; Volt.Telecom.Licensing.Models.LicenseType,Volt.Telecom.Licensing.Models&#34;,&# 34;描述&#34;:&#34;承包商和#34;&#34; IsActive&#34;:假,&#34;标识&#34;:2},&#34;状态&#34;:{& #34; $ REF&#34;:&#34; 3&#34;}&#34; DateIssued&#34;:&#34; 2012-11-18T00:00:00.000&#34;&#34; ValidFor&#34;:1095,&#34; IsActive&#34;:假,&#34;标识&#34;:3},{&#34; $ ID&#34;:&#34; 8&#34 ;, &#34; $ type&#34;:&#34; Volt.Telecom.Licensing.Models.License,Volt.T elecom.Licensing.Models&#34;,&#34; LicenseName&#34;:&#34; Low Voltage&#34;,&#34; LicenseType&#34;:{&#34; $ ref&#34;:&# 34; 5&#34;}&#34;状态&#34; {&#34; $ ID&#34;:&#34; 9&#34;&#34; $类型&#34;:&#34 ; Volt.Telecom.Licensing.Models.State,Volt.Telecom.Licensing.Models&#34;,&#34; Name&#34;:&#34; CA&#34;,&#34; LongName&#34;:& #34;加州&#34;&#34; IsActive&#34;:假,&#34;标识&#34; 35},&#34; DateIssued&#34;:&#34; 2012-11-18T00: 00:00.000&#34;&#34; ValidFor&#34;:1095,&#34; IsActive&#34;:假,&#34;标识&#34:4},{&#34; $ ID&# 34;:&#34; 10&#34;,&#34; $ type&#34;:&#34; Volt.Telecom.Licensing.Models.License,Volt.Telecom.Licensing.Models&#34;,&#34 ; LicenseName&#34;:&#34;承包商和#34;&#34;授权类型&#34; {&#34; $ REF&#34;:&#34; 7&#34;}&#34;状态& #34; {&#34; $ REF&#34;:&#34; 9&#34;}&#34; DateIssued&#34;:&#34; 2012-11-18T00:00:00.000&#34 ;,&#34; ValidFor&#34;:1095,&#34; IsActive&#34;:假,&#34;标识&#34;:5},{&#34; $ ID&#34;:&#34 ; 11&#34;,&#34; $ type&#34;:&#34; Volt.Telecom.Licensing.Models.License,Volt.Telecom.Licensing.Models&#34;,&#34 ; LicenseName&#34;:&#34; General Contractors&#34;,&#34; LicenseType&#34;:{&#34; $ ref&#34;:&#34; 2&#34;},&#34;状态&#34; {&#34; $ REF&#34;:&#34; 9&#34;}&#34; DateIssued&#34;:&#34; 2012-11-18T00:00:00.000&# 34;,&#34; ValidFor&#34;:1095,&#34; IsActive&#34;:假,&#34;标识&#34;:6}]

2 个答案:

答案 0 :(得分:0)

编辑:从v 1.3.1开始,Breeze现在支持继承。

问题可能是Breeze还不支持继承。这里有一个UserVoice suggestion。请投票。我们非常重视这些建议。

要确认这是您的问题,您是否可以展平结构,以便您不需要继承并查看问题是否消失。

答案 1 :(得分:0)

我认为如果某个东西在服务器到客户端之间消失了,因为Breeze保留了空数据列,这可能会破坏数据结构并使淘汰绑定功能失效。
在选择最小化Breeze性能时,您可以根据需要将属性放在下层 1 - BreezeWebApiConfig.cs级别
2 - 控制器级别
3 - 或HttGet级别

这个属性对我有用:
var jsonx = Breeze.WebApi.BreezeConfig.Instance;
jsonx.GetJsonSerializerSettings()。NullValueHandling = Newtonsoft.Json.NullValueHandling.Include;

相关问题