1

My goal is to persist a simple relationship between two breeze entities on my OData server.

I've setup a server, I am able to:

  • insert data
  • query data
  • create a relation between two entities client-side
  • BUT not able to persist the relationship server-side.

Any ideas of what I'm missing?

Below is my setup details


  • I'm using OData/MongoDB backend running JayData on Node.js (virtualBox, ubuntu 12.04, node.js, MongoDB)
  • I have created a simple data model with a user and a person entity based on this stack overflow question. This is the JayData datamodel definition

>

$data.Class.define("$org.Types.user", $data.Entity, null, {
    Id: {type: "id", key: true, computed: true, nullable: false },
    Person: { type: "$org.Types.person", inverseProperty: "User", required:true },
    EmailAddress: { type: "string"},
    Password: { type: "string"}
}, null)

$data.Class.define("$org.Types.person", $data.Entity, null, {
    Id: {type: "id", key: true, computed: true, nullable: false },
    User: { type: "$org.Types.user", inverseProperty: "Person"},
    FirstName: { type: "string"},
    LastName: { type: "string"}
}, null);


$data.Class.defineEx("$org.Types.Context", [$data.EntityContext, $data.ServiceBase], null, {
    User: {type: $data.EntitySet, elementType: $org.Types.user } ,
    Person: {type: $data.EntitySet, elementType: $org.Types.person }
});

exports = $org.Types.Context;
  • I'm able to query the data using Breeze.js
  • I'm able to insert User and Person entities using Breeze.js
  • I'm able to set a relation in Breeze and I see the changes to the entities
  • I've updated Breeze.js to 1.3.3

>

var manager = new breeze.EntityManager('api/');
// other breeze initialization stuff, metadata etc.

var person = manager.createEntity('Person', undefined, breeze.EntityState.Detached);
var user = manager.createEntity('User', undefined, breeze.EntityState.Detached);

// set other fields, name, email, password
user.Person(user);
manager.addEntity(user);
manager.addEntity(person);

// save the changes
manager.saveChanges().then(function() { // etc

But I only see two OData post for the two entities but nothing to tie the two entities together

  • I ruled out the OData adapter as both the WebAPI and OData adapter call the same functions in the Breeze code base and there are multiple Breeze/WebAPI that demonstrate the ability to query and inserted related entities.
  • I haven't seen a Breeze.js / OData sample that seems to address this.
  • The closest example that works is a datajs code sample. It posts the related entity to /$links/ to create the relationship (something I don't see in the OData adapter of neither Breeze.js or JayData).
4

1 回答 1

0

我从未将 Breeze 与 JayData 服务器一起使用,但我已经使用JayData 客户端库通过 OData 保存数据一年了,它适用于静态模式(您使用 JaySvcUtil 生成它)和动态模式(从服务器获取元数据在运行时)也是如此。ű

对于重要的事情:

  • 使用类型化的实体,而不是 POCO 对象
  • 设置关系的所需侧以使项目持久化

    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script src="http://include.jaydata.org/datajs-1.0.3.min.js"></script>
    <script src="http://include.jaydata.org/jaydata.js"></script>
    <script>
            var url = 'http://localhost:54001/personService';
    
            $data.service(url, function(contextFactory, contextType){
                var db = contextFactory();
                db.onReady(function(){
    
                    var user = new db.User.elementType({ EmailAddress: 'joe.smith@company.org'});
                    var person = new db.Person.elementType({ FistName: 'Joe', LastName: 'Smith', User: user }); //set the value to the required side of the relationship to have your entities saved in the batch
                    db.Person.add(person)
    
                    db.saveChanges(function () {
                        db.Person.include('User').toArray(function (p) { console.log(p);});
                    });
                })
            });
    

免责声明:我是 JayData 开发团队的成员,因此您也可以在服务器端为您提供帮助。

于 2013-05-17T14:28:26.497 回答