after a lot of help yesterday, I came up against a known error in asp.net4 beta - I upgraded to VS2012 RC Express (4.5), and now I'm getting an internal server error, and I can't see why. I'm creating a web API:
Model
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; }
}
}
I then used the Add Controller wizard, selected "Template: API controller with read/write actoins, using Entity Framework", chose tblCustomerBooking as my Model Class, and clicked , which is:
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) automatically generated by Visual Studio 2012 Express is:
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();
}
}
}
I added a breakpoint at "return db....." above, and checked the Watch part in VS - it clearly shows the object, with the customer, and the associated rentals:
However if I allow the script to continue, I just get an http500 error (as shown in Fiddler below):
Is there any more code I can add into the controller to allow me to see why it is erroring? Or can anyone see what may be wrong? VS appears to retrieve it ok, as shown in the first screenshot, but doesn't seem to be able to send it out.
Thanks for any help or pointers,
Mark
Update
Hi - am I simply asking too much of the API? Is it not possible (out of the box) for it to simply return objects with one to many relationships? Can it only really produce a single object list?
Thanks, Mark