creating/updating breeze entities with one-to-one relationship

北战南征 提交于 2019-12-01 11:42:03
Ward

You've uncovered a bug ... or so it seems to me. I have filed it and we'll let you know when it is fixed.

On a different note, your BreezeJS client code is a little more tortured than it needs to be. Why create an uninitialized detached entity ... and then add it?

The line user.Person(user); doesn't seem right at all. That should fail. I think you meant user.Person(person);. But you won't need that either.

Try this:

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

// adds new, initialized Person entity
var person = manager.createEntity('Person', {
        // perhaps init fields: firstName, lastName
    });

// adds new, initialized User entity, associated w/ parent Person
var user = manager.createEntity('User', {
       personId: person.Id(), // SETS THE KEY TO PARENT PERSON
       // perhaps init fields: emailAddress, password (really!?!)
});

manager.saveChanges().then(function() { ... etc.

Update 8 May 2013

Yes, that commit is the fix on the BreezeJS side. We'll release it soon ... although of course you are free to grab an intermediate commit. I wouldn't ... because intermediate, untagged commits haven't been properly QA'd.

I think you will still have a problem ... this time on the EntityFramework side. 1-1 is pretty trick in Code First; you may want to look at this StackOverflow thread.

To be specific to your case, I think you need to change your definition of User to this:

public class User {
    [Key]
    [ForeignKey("Person")]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int Id { get; set; }

    //[Required]
    public virtual Person Person { get; set; }

    [Required]
    public string EmailAddress { get; set; }

    [Required]
    public string Password { get; set; }
}

You shouldn't need the "Fluent API" mapping for User that you show above and it might get in the way.

// DELETE THIS !!!
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
     modelBuilder.Entity().HasRequired(x => x.Person);
}

FWIW, the Order/InternationalOrder relationship in DocCode is virtually the same as your Person/User. The test, "can save a new Northwind Order & InternationalOrder [1..(0,1) relationship]", in the saveNorthwindTests.js confirms the model definition and the Breeze fix.

Hope this helps.

Breeze v 1.3.3 is now available on the Breeze website and we did fix an issue with 1-1 mappings. Could you confirm whether this corrects your issue?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!