0
votes

C# Web.Api Odata APplication

I’m implementing Odata V4 for the first time and would like to use my own custom class instead of the data class from the designer.

Here is what I did:

I created a Data project in Visual Studios and added my SQL Linq data table “Video”

I created the data context as follows:

public class VideoDataContext : DbContext
{
public VideoDataContext(): base("name=VideoData")
{
DbSet<VideoEf> Videos { get; set; }
}

And my custom class as follows:

    [Serializable]
    [DataContract]
    public class VideoEf : Repository
    {
        [DataMember]
        public int Id { get; set; }

        [DataMember]
        public string Isrc { get; set; }
}

And model builder:

public Microsoft.OData.Edm.IEdmModel GetEdmModel()
{
ODataModelBuilder builder = new ODataConventionModelBuilder();
EntityTypeConfiguration<VideoEf> titleType = builder.EntityType<VideoEf>();
 builder.EntitySet<VideoEf>("Video");
return builder.GetEdmModel();
}

And in my video controller:

public class VideoController : ODataController
{
VideoDataContext  db = new VideoDataContext  ();
[EnableQuery(PageSize = 20, MaxExpansionDepth = 5)]
public IHttpActionResult Get()
{
return Ok(db.Videos.AsQueryable());
}

When I make the call to get the video entities I keep getting a ” 406 Not Acceptable” error message

How can I ensure that the data returned from the database is mapped to my custom model ?

Is my model builder correct?

what could be causing the above error?

1

1 Answers

0
votes

You don't need to return IQueryable because you have EnableQuery attribute, just return DbSet.

You also don't need any wcf attribute and EntityTypeConfiguration<VideoEf> titleType = builder.EntityType<VideoEf>();

Then it should just work. Hope it helps.

Edit My mistake for IQueryable, I also use it.