Breeze Webapi更新投影 [英] Breeze Webapi Update Projections

查看:81
本文介绍了Breeze Webapi更新投影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我最初的WebApi筛选和分页投影难题之后继续



微风的$ filter投影线程



我现在正尝试更新后端数据库,但收效甚微。



说明事实:

  public IQueryable< ConsigneDTO>收货人(字符串refname)
{
IQueryable< ConsigneDTO> q = this.db.Consignes
.Where(x => x.Refname == refname)
.Select(f => new ConsigneDTO {Refname = f.Refname,收货人= f。收货人,地址1 = f。地址1,地址2 = f。地址2,地址3 = f。地址3});
返回q;
}

从Breeze放回数据的最简单方法是什么?



我看到我的更改后的数据同时在两个覆盖中返回

 受保护的覆盖布尔值BeforeSaveEntity( EntityInfo entityInfo)
{
}

受保护的重写Dictionary< Type,List< EntityInfo>> BeforeSaveEntities(Dictionary< Type,List< EntityInfo>> saveMap)
{
}

但是没有简单的方法将其推入基础上下文,这是源自Data.Services.Client.DataServiceContext的WCF服务。



我怎么称呼

  Context.UpdateChanges(EntityInfo); 

  Context.UpdateChanges(SaveMap); 

特别考虑EntityInfo和SaveMap包含DTO吗?



我是否必须完全忽略SaveChanges()机制,并执行自己的CRUD调用?我什至看不到任何关于更新投影的非微风示例,这使我感到这是根深蒂固的。



如前所述,我可以改变大多数这个项目的事情。我唯一的问题是数据库在另一台服务器上,并且具有无法公开的列,而我在表上的唯一方法是EF5。



我选择使用OData-v3 / WCF-Data-Services来包装EF并通过Web服务器上的微风控制器访问它们。



到目前为止,在SPA上阅读,查询,过滤和分页都非常有效,但是只有R C_UD,我才更愿意对这个项目进行重做。 / p>

亲切问候
Mike

解决方案

Breeze EntityManager。 saveChanges调用旨在将更改保存到先前查询的实体。 Breeze可以自动执行此过程,因为它具有有关这些实体类型的元数据,并且能够在服务器上构造适当的代码以持久化它们。对于您而言,查询返回的不是实体,除非将其转换为实体,否则Breeze将无法持久保存它们。因此,您可以尝试几种方法。



第一个方法是在客户端上获取您的DTO时,从其中动态构建部分实体,如果需要进行任何更改,可以稍后将其完全解析。请参阅John Papa的 Code Camper 示例,以查看此示例。附带说明一下,如果您的DTO实际上在投影中包含Breeze实体,那么甚至没有必要。



另一种方法是在尝试保存这些时完全绕过Breeze。只需直接使用Breeze ajax适配器,然后将数据直接发布到所需的任何端点即可。显然,在这种情况下,您将需要编写所有必要的服务器端代码以持久保存已发布的数据。


Continuing on after my initial WebApi Filtering and paging a projection Dilemma

Breeze $filter projections thread

I am now trying to update my backend database with little success.

Giving the Fact that I am:

    public IQueryable<ConsigneDTO> Consignees(string refname)
    {
        IQueryable<ConsigneDTO> q = this.db.Consignes
             .Where(x => x.Refname == refname)
                  .Select(f => new ConsigneDTO {Refname = f.Refname, Consignee = f.Consignee, Address1 = f.Address1, Address2 = f.Address2, Address3 = f.Address3});
        return q;
    }

Whats the easiest way to put this data back from Breeze?

I see my changed data coming back in both overrides

    protected override bool BeforeSaveEntity(EntityInfo entityInfo)
    {
    }

    protected override Dictionary<Type, List<EntityInfo>> BeforeSaveEntities(Dictionary<Type, List<EntityInfo>> saveMap)
    {
    }

But no easy way to push these into the underlying context which is a WCF service stemming from Data.Services.Client.DataServiceContext.

How do I call

  Context.UpdateChanges(EntityInfo);

or

  Context.UpdateChanges(SaveMap);

Especially Considering EntityInfo and SaveMap contains DTO?

Must I completely ignore the SaveChanges() mechanism and do my own CRUD calls? I don't even see any good non-breeze examples on updating projections, which make me feel this is deep rooted.

As explained in prior thread, I can change most things about this project. The only issues I have are the databases are on another server and has columns that cannot be public and my only way at the tables are EF5.

I've chosen to wrap EF thinly using OData-v3 / WCF-Data-Services and access them from my breeze controller on the web server.

So far reading\querying\filtering and paging are all working great on SPA, but with only the R of "C_UD", I'm more then willing to rework this project.

Kind Regards Mike

解决方案

The Breeze EntityManager.saveChanges call is intended to save changes to entities that were previously queried. This process can be automated by Breeze because it has metadata regarding these entity types and is able to construct the appropriate code on the server to persist them. In your case what you are returning from your queries are not 'entities' and unless you can turn them into entities Breeze will not be able to persist them. So you have several approaches that you can try.

The first is to take your DTO's when retrieved on the client and dynamically construct "partial" entities from them that can later be fully resolved if any changes need to be made. See John Papa's Code Camper sample to see an example of this. As a side note, if your DTO actually includes Breeze entities within the projection then even this is not necessary.

Another approach is to bypass Breeze completely when attempting to save these. Simply use the Breeze ajax adapter directly and post your data directly to whatever endpoint you'd like. Obviously, in this case, you will need to write all of the server side code necessary to persist the posted data.

这篇关于Breeze Webapi更新投影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆