0

I am trying to pass a complex type to WebApi having this on my ApiController :

    [HttpPost]
    public void DoSomeCrud(JObject data)
    {
        ComplexModel item = data.ToObject<ComplexModel>();
        // Do some logic here
    }

My issue is that one of the properties I have inside my ComplexModel is an Entity Framework entity. I don't have problems passing that entity if detached, however as soon as I get that entity from DbContext the model cannot be passed to WebApi as expected.

My question is.. : Is there anyway to detach my entity preserving my references to foreign keys ? Because I need those references on the WebApi side.

Thanks

4

1 回答 1

0

使用实体框架中的模型作为 Web Api 的数据传输对象 (Dto) 并不是最佳实践,因为您可能会遇到序列化问题,因为来自 EF 的模型实际上是支持延迟加载和导航属性的代理(如果您不分离它) )。

最佳实践是,为了关注点分离,您应该定义自己的 Dto 对象,而不是直接从 EF 使用实体模型。

简单的例子,如果你有Customer实体,你也应该有CustomerDto实体,它可以投射Customer你想要的任何属性。

于 2013-09-03T15:55:16.220 回答