Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

OData Having difficulty building a simple OData service.

$
0
0

Trying to build an OData service based on the Entity Relations in OData v4 Using ASP.NET Web API 2.2

I did get this tutorial running.

Now want to build with one of my tables.

Model:

namespace QBOData.Models
{
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

    [Table("People")]
    public partial class Person
    {
        //public Person()
        //{
        //    PIndexes = new HashSet<PIndex>();
        //}
        [Key]
        public int Id { get; set; }

        [Required]
        public string LName { get; set; }

        public string FName { get; set; }

        public string MName { get; set; }

        public string Suffix { get; set; }

        public string Biography { get; set; }

        //public virtual ICollection<PIndex> PIndexes { get; set; }

    }
}



Model Context:

    class QBOContext : DbContext
    {
        public QBOContext() : base("name=QBO")
        {
        }

        public DbSet<Person> People { get; set;  }
    }

Seeded the Db with

 

        protected override void Seed(QBOData.Models.QBOContext context)
        {
            //  This method will be called after migrating to the latest version.

            context.People.AddOrUpdate(
                p => p.Id,
                new Person { Id = 1, LName = "Turabian", FName = "Kate", MName = "L.", Suffix = "", Biography = "" },
                new Person { Id = 2, LName = "Gonzalez", FName = "Justo", MName = "L", Suffix = "", Biography = "" },
                new Person { Id = 3, LName = "Augustine", FName = "", MName = "", Suffix = "", Biography = "" },
                new Person { Id = 4, LName = "Delete Me", FName = "", MName = "", Suffix = "", Biography = "" },
                new Person { Id = 5, LName = "Delete Me", FName = "", MName = "", Suffix = "", Biography = "" },
                new Person { Id = 6, LName = "Edwards", FName = "Jonathan", MName = "", Suffix = "", Biography = "" },
                new Person { Id = 7, LName = "Noll", FName = "Mark", MName = "A.", Suffix = "", Biography = "" }
                );

        }



The Database is updated and correct.

---

When attempting to run

namespace QBOData
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            // OData Routing
            ODataModelBuilder builder = new ODataModelBuilder();
            builder.EntitySet<Person>("People");
            config.MapODataServiceRoute(
                routeName: "ODataRoute",
                routePrefix: "odata",
                model: builder.GetEdmModel()); <<== Exception "The entity 'Person' does not have a key defined."
        }
    }
}

Please note the exception in the above code section.

I have tried to add the key as 

builder.EntitySet<Person>("People").EntityType.HasKey(p => p.Id);

This did not work either.

Need A tutorial that I can use for a base.

Thanks for the help


Viewing all articles
Browse latest Browse all 4850

Trending Articles