OData Controller: Wrong methods called (.NET Core)

Viewed 16
    public class Account
    {
        [Key]
        [StringLength(80)]
        public string AccountID { get; set; } = string.Empty;

        [StringLength(255)]
        public string Name { get; set; } = string.Empty;

        [ForeignKey(nameof(AccountID))]
        public virtual ICollection<Relation> Relations { get; set; }
    }
    public class Role
    {
        [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
        public long RoleID { get; set; } = -1;

        [StringLength(80)]
        public string RoleName { get; set; } = string.Empty;
    }
    public class Relation
    {
        [Key]
        [StringLength(80)]
        public string AccountID { get; set; } = string.Empty;

        [Required]
        public long RoleID { get; set; } = 0;

        [ForeignKey(nameof(RoleID))]
        public virtual Role Role { get; set; }
    }
    public class AccountsController : ODataController
    {
        private readonly DS2DbContext _context;

        public AccountsController(DS2DbContext context)
        {
            _context = context;
        }

        [EnableQuery(PageSize = 20)]
        public IQueryable<Account> Get()
        {
            return _context.Accounts;
        }

        [EnableQuery]
        public SingleResult<Account> Get([FromODataUri] string ID)
        {
            var result = _context.Accounts.Where(
                e => string.Compare(e.AccountID, ID, StringComparison.InvariantCultureIgnoreCase) == 0);
            return SingleResult.Create(result);
        }
    }

The controller is called by a grid which is querying a list of Account records as well as code that reads a single Account record with a given account id.

List query:

https://localhost:44393/DS/Accounts?$count=true&$expand=UserInfo,Relations($expand=Role)&$skip=0&$top=20

Single record query:

https://localhost:44393/DS/Accounts('bt0388')?$expand=UserInfo,Relations($expand=Role)

As long as the SingleResult<Account> Get() method is commented out, the list query ends up in IQueryable<Account> Get() as it should and the query works fine.

As soon as I uncomment SingleResult<Account> Get(), the list query ends up in the SingleResult<Account> Get() and fails.

The single record query never reaches SingleResult<Account> Get(). Omitting the $expand parameter doesn't change anything.

What is going wrong here, and how do I fix it?

0 Answers
Related