Entity framework 角度2在可观测订阅内丢失数据
在我的应用程序中,我有一个Web API控制器,它应该向我的数据库中添加一个类型为“Source”的实体,然后返回创建的源。Source包含“SourceFields”的列表/集合,这些字段本身由许多字段组成。Source还继承了一个用于变更跟踪的基本实体,该实体包含诸如date created、date modified等字段 使用Postman,我测试了我的API,并验证了它在发布源代码时正确返回,并且它包含了所有创建的对象。但是,当我将Angular 2 HTTP与RxJS Observables一起使用时,返回的值删除了所有基本实体字段以及sourcefields。我在Angular 2中使用了一个源类来执行操作,但我在调用中的任何地方都没有使用它,所以我认为它没有被类型化或强制化?获得正确返回的源响应(即包括基本实体属性)的唯一方法是在API POST方法中将sourcefields设置为null 这个数据服务可以完美地处理简单的实体。例如,我有一个客户机实体,它不包含像sourcefields这样的集合,它只返回基本实体字段 有什么想法吗 我的API POST方法:Entity framework 角度2在可观测订阅内丢失数据,entity-framework,angular,asp.net-core,angular2-http,asp.net-core-webapi,Entity Framework,Angular,Asp.net Core,Angular2 Http,Asp.net Core Webapi,在我的应用程序中,我有一个Web API控制器,它应该向我的数据库中添加一个类型为“Source”的实体,然后返回创建的源。Source包含“SourceFields”的列表/集合,这些字段本身由许多字段组成。Source还继承了一个用于变更跟踪的基本实体,该实体包含诸如date created、date modified等字段 使用Postman,我测试了我的API,并验证了它在发布源代码时正确返回,并且它包含了所有创建的对象。但是,当我将Angular 2 HTTP与RxJS Observa
// POST api/Sources
[HttpPost]
public IActionResult Post([FromBody]Source source)
{
if (source == null)
{
return BadRequest();
}
dbContext.Sources.Add(source);
dbContext.SaveChanges();
//Hack to get source to return properly
//source.SourceFields = null;
return CreatedAtRoute("GetSource", new { id = source.SourceId }, source);
}
角度2数据服务方法:
public Add (action: string, itemToAdd: any): Observable<any> {
var toAdd = JSON.stringify(itemToAdd);
return this._http.post(this.actionUrl + action, toAdd, { headers: this.headers })
.map(res => res.json())
.catch((error: any) => Observable.throw(error.json().error || 'Server Error'));
}
如果有人想看我的实体模型或任何东西,我可以添加它们
this.addEditSource.sourceFields = this.sourceFields;
this._dataService.Add('Sources', this.addEditSource).subscribe(source => {
this.sources.push(source)
},
error => console.log(error));