0

I'm using an Odata controller. On the response I'd like for siteUsers and siteUser.Login to serialize. Currently only the top level of siteUsers is serializing. What is the best way to do this?

namespace SOW.Controllers {

public class SiteUsersController : ODataController
{


    // POST: odata/SiteUsers
    public IHttpActionResult Post(SiteUser siteUser)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        db.SiteUsers.Add(siteUser);
        db.SaveChanges();

        return Created(siteUser);
    }

}

public partial class SiteUser
{
    public int SiteUsersId { get; set; }
    public int SiteId { get; set; }
    public int LoginId { get; set; }
    public string CreatedBy { get; set; }
    public Nullable<System.DateTime> CreateDate { get; set; }
    public string UpdatedBy { get; set; }
    public Nullable<System.DateTime> UpdatedDate { get; set; }

    public virtual Login Login { get; set; }
    public virtual Site Site { get; set; }
}




 public partial class Login
    {
        public Login()
        {
            this.SiteUsers = new HashSet<SiteUser>();
            this.SOWTransactions = new HashSet<SOWTransaction>();
        }

        public int LoginId { get; set; }
        public string UserName { get; set; }
        public string DisplayName { get; set; }
        public string eMail { get; set; }
        public Nullable<bool> active { get; set; }
        public Nullable<System.DateTime> LastLogon { get; set; }

        public virtual ICollection<SiteUser> SiteUsers { get; set; }
        public virtual ICollection<SOWTransaction> SOWTransactions { get; set; }
    }
ffejrekaburb
  • 656
  • 1
  • 10
  • 35

1 Answers1

0

The reason that the Login is not showing is because it's of an entity type that's different from SiteUser so having a Login type property in the SiteUser class will make it a navigation property of the SiteUser entity.

In order to have the service return Login in the response requesting SiteUsers, you need to make sure you have [EnableQuery] attribute on the Get action in the SiteUsersController

[EnableQuery]
public IQueryable<SiteUser> Get()
{
    return db.SiteUsers;
}

and send such request to the service

GET http://host/service/odata/SiteUsers?$expand=Login
Yi Ding - MSFT
  • 2,864
  • 16
  • 16