ASP.Net Web API mostrando corretamente no VS, mas dando HTTP500
Depois de muita ajuda ontem, eu encontrei um erro conhecido no asp.net4 beta - eu atualizei para o VS2012 RC Express (4.5), e agora estou recebendo um erro interno do servidor, e não consigo ver o porquê. Estou criando uma API da web:
Modelo
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations.Schema;
namespace MvcApplication6.Models
{
public class tblCustomerBooking
{
[Key()]
public int customer_id { get; set; }
public string customer_name { get; set; }
public string customer_email { get; set; }
public virtual ICollection<tblRental> tblRentals { get; set; }
}
public class tblRental
{
[Key()]
public int rental_id { get; set; }
public int room_id { get; set; }
public DateTime check_in { get; set; }
public DateTime check_out { get; set; }
public decimal room_cost { get; set; }
public int customer_id { get; set; }
[ForeignKey("customer_id")]
public virtual tblCustomerBooking tblCustomerBooking { get; set; }
}
}
Em seguida, usei o assistente Adicionar Controlador, selecionei "Template: API controller with read / write actoins, usando Entity Framework", escolhi tblCustomerBooking como minha Model Class e cliquei, que é:
using System.Data.Entity;
namespace MvcApplication6.Models
{
public class BookingsContext : DbContext
{
public BookingsContext() : base("name=BookingsContext")
{
}
public DbSet<tblCustomerBooking> tblCustomerBookings { get; set; }
}
}
My Controller (BookingsController.cs) gerado automaticamente pelo Visual Studio 2012 Express é:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using MvcApplication6.Models;
namespace MvcApplication6.Controllers
{
public class BookingsController : ApiController
{
private BookingsContext db = new BookingsContext();
// GET api/Bookings
public IEnumerable<tblCustomerBooking> GettblCustomerBookings()
{
return db.tblCustomerBookings.AsEnumerable();
}
}
}
Eu adicionei um ponto de interrupção em "return db ....." acima e verifiquei a parte Watch no VS - ele mostra claramente o objeto, com o cliente e os aluguéis associados:
No entanto, se eu permitir que o script continue, acabei de receber um erro http500 (como mostrado no Fiddler abaixo):
Existe mais algum código que eu possa adicionar ao controlador para me permitir ver por que ele está errando? Ou alguém pode ver o que pode estar errado? VS aparece para recuperá-lo ok, como mostrado na primeira tela, mas não parece ser capaz de enviá-lo para fora.
Obrigado por qualquer ajuda ou ponteiros,
Marca
Atualizar
Oi, estou simplesmente perguntando demais sobre a API? Não é possível (fora da caixa) simplesmente retornar objetos com um para muitos relacionamentos? Só pode realmente produzir uma única lista de objetos?
Obrigado, Mark