【问题标题】:navigation properties not saving导航属性不保存
【发布时间】:2013-03-04 21:11:19
【问题描述】:

我有一个简单的对象模型 公共类许可证 {

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [ScaffoldColumn(false), StringLength(20)]
    public string CreationUserId { get; set; }

    [ScaffoldColumn(false), StringLength(20)]
    public string LastModifiedUserId { get; set; }

    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 State
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [ScaffoldColumn(false), StringLength(20)]
    public string CreationUserId { get; set; }

    [ScaffoldColumn(false), StringLength(20)]
    public string LastModifiedUserId { get; set; }

    [StringLength(2)]
    [Required]
    public string Name { get; set; }

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

public class LicenseType
{

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [ScaffoldColumn(false), StringLength(20)]
    public string CreationUserId { get; set; }

    [ScaffoldColumn(false), StringLength(20)]
    public string LastModifiedUserId { get; set; }

    [StringLength(100), Required]
    public string Description { get; set; }
}

我正在使用热毛巾模板微风,杜兰达尔,淘汰赛。 我有一个简单的添加视图模型

var _licenseAdded = false;
var vm = {

    states: ko.observableArray(context.states),
    licenseTypes: ko.observableArray(context.licenseTypes),

    viewAttached: function () {
        var self = this;
        $('input[name^="date"]').datepicker();
        $('#validFor').spinner({
            min: 365,
            max: 3650,
            step: 30
        });
        log('add Attached', null, true);

    },

    activate: function () {
        var self = this;
        self.original = context.manager.createEntity('License', { licenseName: 'Testing321', dateIssued: moment().format('L') }, null);
        log('add Activated', null, true);


    },
    canDeactivate: function () {
        if (_licenseAdded === false) {
            return app.showMessage('Are you sure you want to leave this page?', 'Navigate', ['Yes', 'No']);
        } else {
            return true;
        }
    },
    saveChanges: function () {

        $.when(context.saveNewLicense()).done(function () {
            _licenseAdded = true;

        });
        router.navigateTo('home');
    },
    original: undefined

};
return vm;

这是我的 add.html,一切都很好,并且在保存之前运行良好。

当我调用 saveChanges 时,发送到控制器的 saveBundle 没有附加导航属性,允许存储正确的 State 和 LicenseType 我只得到: 保存包 { “实体”:[ { “身份证”:-1, “CreationUserId”:空, “LastModifiedUserId”:空, "LicenseName": "新的测试用", "发布日期": "2013-03-11T04:00:00Z", “有效”:0, “实体方面”:{ "entityTypeName": "许可证:#Volt.Telecom.Licensing.Models", “实体状态”:“添加”, “原始值映射”:{}, “自动生成密钥”:{ "propertyName": "Id", “autoGeneratedKeyType”:“身份” } } } ], “保存选项”:{ “allowConcurrentSaves”:假 } }

不要以为我能得到比这更多的香草。为什么会发生这种情况?当我在客户端上调试时,状态和 licenseType 导航属性都是正确的并且具有正确的值。

【问题讨论】:

    标签: breeze


    【解决方案1】:

    我认为问题在于您的 EF 模型使用“独立关联”而不是“外键关联”(外键关联是 EF 默认设置)。我们确实需要更好地记录这个假设。

    此要求的原因是可以绕过但会大量丢失功能,因为客户端上存在外键允许微风自动修复可能单独查询的实体之间的关系。

    有关更多背景信息,请参阅以下 MSDN 文章:foreign-keys-in-the-entity-framework

    【讨论】:

    • 这是我习惯不这样做的问题,现在我在文档中忽略了它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-30
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多