Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 防止ASP.NET Web API返回EntityKey_Asp.net Mvc_Entity Framework_Controller - Fatal编程技术网

Asp.net mvc 防止ASP.NET Web API返回EntityKey

Asp.net mvc 防止ASP.NET Web API返回EntityKey,asp.net-mvc,entity-framework,controller,Asp.net Mvc,Entity Framework,Controller,在我的Web API控制器中,我将结果返回到如下方法调用: // GET api/profile/5 public VCompleteProjectProfile GetBasicProjectProfile(int id) { return _dbss.GetBasicProfile(id); } 以下是实际结果: { "$id":"1", "Id":1, "ProjectName":"Caribbean Challenge", "IsMrRcSele

在我的Web API控制器中,我将结果返回到如下方法调用:

// GET api/profile/5
public VCompleteProjectProfile GetBasicProjectProfile(int id)
{
    return _dbss.GetBasicProfile(id);
}
以下是实际结果:

{
    "$id":"1",
    "Id":1,
    "ProjectName":"Caribbean Challenge",
    "IsMrRcSelected":true,
    "IsMrdProject":true,
    "RegionName":"North America",
    "EntityKey":{
        "$id":"2",
        "EntitySetName":"VCompleteProjectProfile",
        "EntityContainerName":"MrdViewEntities",
        "EntityKeyValues":[
            {"Key":"Id","Type":"System.Int32","Value":"1"},
            {"Key":"ProjectName","Type":"System.String","Value":"Caribbean Challenge"},
            {"Key":"IsMrRcSelected","Type":"System.Boolean","Value":"True"}
        ]
    }
}
是否可以抑制
EntityKey
?如果是,怎么做?这是MVC4

谢谢
Eric

为结果创建DTO/POCO,而不是返回实际的实体对象,例如

var entity = _dbss.GetBasicProfile(id);
return new ProfileDto()
{
    Id = entity.Id,
    ProjectName = entity.ProjectName,
    ....
};
您甚至可以扩展实体类型,以包含一个名为
ToDto
的函数,该函数将为您完成此操作,例如

public partial class ProfileEntity
{
    public ProfileDto ToDto()
    {
        return new ProfileDto()
        {
            Id = this.Id,
            ProjectName = this.ProjectName,
            ....
        };
    }
}

....

var entity = _dbss.GetBasicProfile(id);
return entity.ToDto();
一般的经验法则是只返回需要的数据,不要因为方便就抄近路


此外,如果你发现自己不得不到处这样做,那么看看类似的东西,会让你的生活变得更轻松。

为你的结果创建DTO/POCO,而不是返回实际的实体对象,例如

var entity = _dbss.GetBasicProfile(id);
return new ProfileDto()
{
    Id = entity.Id,
    ProjectName = entity.ProjectName,
    ....
};
您甚至可以扩展实体类型,以包含一个名为
ToDto
的函数,该函数将为您完成此操作,例如

public partial class ProfileEntity
{
    public ProfileDto ToDto()
    {
        return new ProfileDto()
        {
            Id = this.Id,
            ProjectName = this.ProjectName,
            ....
        };
    }
}

....

var entity = _dbss.GetBasicProfile(id);
return entity.ToDto();
一般的经验法则是只返回需要的数据,不要因为方便就抄近路


此外,如果你发现自己不得不到处这样做,那么看看类似的东西,会让你的生活更轻松。

用你希望返回的信息创建一个DTO。使用DTO需要的数据库信息填充DTO。从控制器返回DTO。如果使用POCO而不是旧的基于实体对象的实体,则根本不会有
EntityKey
,只有Id。使用希望返回的信息创建DTO。使用DTO需要的数据库信息填充DTO。从控制器返回DTO。如果使用POCO而不是旧的基于实体对象的实体,则根本没有
EntityKey
——只有Id。