Reputation:
In my Web API controller, I return the results to a method call like so:
// GET api/profile/5
public VCompleteProjectProfile GetBasicProjectProfile(int id)
{
return _dbss.GetBasicProfile(id);
}
And here is the actual result:
{
"$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"}
]
}
}
Is it possible to suppress the EntityKey
? If so, how? This is MVC4.
Thanks Eric
Upvotes: 1
Views: 586
Reputation: 82096
Create a DTO/POCO for your result instead of returning the actual entity object e.g.
var entity = _dbss.GetBasicProfile(id);
return new ProfileDto()
{
Id = entity.Id,
ProjectName = entity.ProjectName,
....
};
You could even extend your entity type to include a function called ToDto
which would do this for you e.g.
public partial class ProfileEntity
{
public ProfileDto ToDto()
{
return new ProfileDto()
{
Id = this.Id,
ProjectName = this.ProjectName,
....
};
}
}
....
var entity = _dbss.GetBasicProfile(id);
return entity.ToDto();
A general rule of thumb is only return data that is needed, don't cut corners just because it's convenient.
Also, if you find yourself having to do this all over the place look at something like AutoMapper, will make your life a lot easier.
Upvotes: 1