【问题标题】:Updating the database record by id(primary key) using breeze in entity framework 6使用实体框架6中的微风按id(主键)更新数据库记录
【发布时间】:2015-10-21 01:53:29
【问题描述】:

在我的项目中,我使用 Entity framework 6 和 Breeze 来获取数据。 在客户端,我正在创建一个修改状态的微风实体,如下所示,

var entity = manager.createEntity(entityName);

entity.entityAspect.setModified();//Modifying the state

entity.Id(id);//pushing existing primary key value

entity.IfInactive(true); //updating the record value from false to true

saveChanges(msg).then(function () { //calling save changes
   console.log('Success.');
});

但在 savechanges 期间,它会给出一个异常,例如“无法设置属性 'Id' 的原始值,因为该属性是实体键的一部分。”

提前致谢!

【问题讨论】:

    标签: entity-framework breeze


    【解决方案1】:

    创建实体时需要使用initializer设置键的值:

    var entity = manager.createEntity(entityName, { Id: id });
    

    【讨论】:

    • 感谢您的回复。我已经使用了该代码,但现在它向我显示了异常,例如“{”UPDATE 语句与 FOREIGN KEY 约束“FK__Following__TestId__76D69450”冲突。数据库 \"TestDB\",表 \"dbo.TestTable\",列 'Id' 中发生冲突。\r\n语句已终止。"} "。
    • 因此我创建了实体作为微风.manager.createEntity(Table_Name, { Id: id },微风.EntityState.Unchanged);所以现在它工作正常。非常感谢......
    • 大家好,我找到了上述问题的解决方案,但我仍然想知道实体框架 6 和微风客户端 v 1.5.4 发生这种情况的确切原因?当我当时使用 EF5 和旧版本的微风客户端时,没有发生这种问题。如果可能的话,请告诉我它的原因。谢谢你!
    猜你喜欢
    • 1970-01-01
    • 2017-08-07
    • 1970-01-01
    • 2019-04-26
    • 2015-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多