I am calling a service with this url: http://localhost:7951/odata/Student but I got error
404 not found
Where am I doing wrong?
My DbContext class is as below:
public class DataContext : DbContext
{
public DataContext()
: base("name=FrameworkConnStr")
{
}
public DbSet<Student> Students { get; set; }
public DbSet<StudentAddress> StudentAddresses { get; set; }
}
[Table("M_DEMIR.STUDENT")]
public partial class Student
{
public Student()
{
StudentAddress = new HashSet<StudentAddress>();
}
[Column("ID"), Key]
public int Id { get; set; }
[Column("NAME", TypeName = "varchar2"), MaxLength(25)]
public string Name { get; set; }
[Column("SURNAME", TypeName = "varchar2"), MaxLength(50)]
public string Surname { get; set; }
public virtual ICollection<StudentAddress> StudentAddress { get; set; }
}
[Table("M_DEMIR.STUDENTADDRESS")]
public partial class StudentAddress
{
[Column("ID"), Key]
public int Id { get; set; }
[Column("STUDENT_ID")]
public int StudentId { get; set; }
[Column("CITY", TypeName = "varchar2"), MaxLength(25)]
public string City { get; set; }
[ForeignKey("StudentId")]
public virtual Student Student { get; set; }
}
Controller Code is as below:
[EnableQuery]
public class StudentController : ODataController
{
DataContext db = new DataContext();
public IHttpActionResult Get()
{
return Ok();
}
}
And WebApiConfig Code is as below:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
var builder = new ODataConventionModelBuilder();
builder.EntitySet<Student>("Students");
builder.EntitySet<StudentAddress>("StudentAddresses");
config.Routes.MapODataServiceRoute("odata", "odata", builder.GetEdmModel());
}
Where am I doing wrong?
[ODataRoutePrefix("Students")]
public class StudentController : ODataController
{
DataContext db = new DataContext();
[EnableQuery(MaxTop = 100)] // MaxTop so you can use the $top in the query string
[ODataRoute]
public IHttpActionResult Get()
{
return Ok(db.Students); // you do want to return something that can be queried
}
}
Change your url to http://localhost:7951/odata/Students (added an s).
Also do not forget to dispose your DbContext
instance by overriding the Dispose
method of your controller.