问题描述
这是我使用EF存储库的微风控制器:
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();
}
}
我想保存这里创建的化合物数据库返回前。我应该调用SaveChanges吗?如何?
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();
因为我收到此错误存储更新,插入或删除语句影响了意外的数量行(0)。实体可能已被修改或删除,因为实体被加载。刷新ObjectStateManager条目。如何解决这个错误?我相信我只是错过了一点调整。
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?
推荐答案
我不知道如何在存储库中声明dbContext。
假设你以这种方式声明:
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; } }
然后你可以添加 _repository.MyDBContext.SaveChanges();
在线之前
Then you can add the _repository.MyDBContext.SaveChanges();
right before the line
return compounds.AsQueryable();
这篇关于Angular / Breeze:如何将BreezeController中的数据直接保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!