Angular/Breeze: how to save the data in BreezeController to database directly

橙三吉。 提交于 2019-12-13 00:23:51

问题


This is my breezeController using EF repository:

[BreezeController]
public class BreezeController : ApiController
{
    private readonly MyRepository _repository;

    public BreezeController()
    {
        _repository = new MyRepository(User);
    }

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

    [HttpGet]
    public IQueryable<Compound> Compounds(int id)
    {
        var compounds = new List<Compound>();
        compounds.add(new Compound() { Name = "cmp1" });
        compounds.add(new Compound() { Name = "cmp2" });
        compounds.add(new Compound() { Name = "cmp3" });

        // Save compounds to database

        return compounds.AsQueryable();
    }
}

I'd like to save the compounds created here to database before returning. Should I call SaveChanges? How?

UPDATE: I tried to bring the objects to client and save. However, I can't seem to use those objects directly as:

cs.compound = compound;
manager.saveChanges();

Because I'm getting this error "Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries". How can I get around this error? I believe I just missed a little tweak.

Instead, I had to create entity as usual, and assign properties one by one like

cs.compound = manager.createEntity("Compound");
cs.compound.name = compound.name;
...
manager.saveChanges();

This is quite cumbersome because I have a lot of properties and nested objects.

So, how can I use the objects created on server to save directly?


回答1:


I don't have an idea of how you declared the dbContext inside the repository. Let's say you have it declared this way :

public MyDBContext { get { return _contextProvider.Context; } }

Then you can add the _repository.MyDBContext.SaveChanges(); right before the line

return compounds.AsQueryable();


来源:https://stackoverflow.com/questions/22432592/angular-breeze-how-to-save-the-data-in-breezecontroller-to-database-directly

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