How to access a child object using OData, WebAPI a

2019-04-16 19:54发布

问题:

From a WebAPI controller, I am returning a list of ProjectEditorDTO objects from an OData request /odata/ProjectEditor?$format=json&$inlinecount=allpages&$top=10:

    [Queryable]
    public virtual IHttpActionResult Get(ODataQueryOptions<Website> odataQueryOptions)
    {
        var userId = UserContext.Identity.UserId;
        try
        {
            var results = odataQueryOptions.ApplyTo(_uow.Repository<Website>()
                .Query()
                .Get()
                .Where(u => u.UserId == userId)
                .OrderBy(o => o.WebsiteName)).Cast<Website>()
                .Select(s => new ProjectEditorDTO()
                {
                    // projection
                    WebsiteId = s.WebsiteId,
                    WebsiteGUID = s.WebsiteGUID,
                    WebsiteName = s.WebsiteName,
                    WebsiteNotes = s.WebsiteNotes,
                    DefaultContentTypeId = s.ContentType.ContentTypeId,
                    DefaultContentType = new ContentTypeDTO
                    {
                        ContentTypeId = s.ContentType.ContentTypeId,
                        Description = s.ContentType.Description
                    }
                });

            var r = results.ToList();
            . . .

JSON Output:

{
  "odata.metadata":"http://localhost:1981/odata/$metadata#ProjectEditor","odata.count":"2","value":[
    {
      "WebsiteId":6,"WebsiteName":"Jobs","WebsiteGUID":"5252fa54-5cbc-4b7e-aa74-4fc2c5c90a6a","WebsiteNotes":"notes","DefaultContentTypeId":67
    },{
      "WebsiteId":7,"WebsiteName":"news","WebsiteGUID":"f0717700-5900-44f7-84a4-9dde9a451285","WebsiteNotes":"notes","DefaultContentTypeId":65
    }
  ]
}

In the projection, I define DefaultContentType but it does not appear in the JSON data returned to the client, even though it is visible at the .ToList() point.

I'm not trying to define a navigation property, so I should not (and can't) $expand it.

I could simply define a DefaultContentTypeId and DefaultContentTypeDescription in the projection, and would solve the problem. However, I would like to know hot to get the DefaultContentType object to appear in the serialized JSON object.

-- Update --

ProjectEditorDTO:

public class ProjectEditorDTO
{
    [ForeignKey("DefaultContentTypeId")]
    public int WebsiteId { get; set; }

    [Required]
    [StringLength(100, MinimumLength = 3)]
    [Display(Name = "Project Name")]
    public string WebsiteName { get; set; }

    public string WebsiteGUID { get; set; }
    //public string WebsitePW { get; set; }
    public string WebsiteNotes { get; set; }

    public int DefaultContentTypeId { get; set; }
    public string DefaultContentTypeDescription { get; set; }

    public virtual ContentTypeDTO DefaultContentType { get; set; }
}

ContentTypeDTO:

public class ContentTypeDTO
{
    public int ContentTypeId { get; set; }

    [Required]
    [StringLength(100, MinimumLength=3)]
    [Display(Name="Content Type")]
    public string Description { get; set; }
}

回答1:

I believe the CLR type ContentTypeDTO is an entity type in your metadata, which can be got by requesting

http://localhost:1981/odata/$metadata

Just change it to a Complex Type. Change

odataConventionModelBuilder.Entity<ContentTypeDTO>() 

to

odataConventionModelBuilder.ComplexType<ContentTypeDTO>();