1

I'm building a self-hosted WebAPI 2 OData 4 service, using Db Context, which is reverse -engineered from an existing database. The context looks like this:

using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using CommonDataService.Models.Mapping;

namespace CommonDataService.Models
{
    public partial class MALContext : DbContext
    {
        static MALContext()
        {
            Database.SetInitializer<MALContext>(null);
        }

        public MALContext()
            : base("Name=MALContext")
        {
        }

        public DbSet<AccountAlia> AccountAlias { get; set; }
        public DbSet<AccountProgram> AccountPrograms { get; set; }
        public DbSet<AccountRolePerson> AccountRolePersons { get; set; }
        public DbSet<Account> Accounts { get; set; }
        public DbSet<ChangeMeasure> ChangeMeasures { get; set; }
        public DbSet<Country> Countries { get; set; }
        public DbSet<Industry> Industries { get; set; }
        public DbSet<Offering> Offerings { get; set; }
        public DbSet<Person> People { get; set; }
        public DbSet<Program> Programs { get; set; }
        public DbSet<RegionAlia> RegionAlias { get; set; }
        public DbSet<Region> Regions { get; set; }
        public DbSet<Role> Roles { get; set; }
        public DbSet<Service> Services { get; set; }
        public DbSet<Tool> Tools { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new AccountAliaMap());
            modelBuilder.Configurations.Add(new AccountProgramMap());
            modelBuilder.Configurations.Add(new AccountRolePersonMap());
            modelBuilder.Configurations.Add(new AccountMap());
            modelBuilder.Configurations.Add(new ChangeMeasureMap());
            modelBuilder.Configurations.Add(new CountryMap());
            modelBuilder.Configurations.Add(new IndustryMap());
            modelBuilder.Configurations.Add(new OfferingMap());
            modelBuilder.Configurations.Add(new PersonMap());
            modelBuilder.Configurations.Add(new ProgramMap());
            modelBuilder.Configurations.Add(new RegionAliaMap());
            modelBuilder.Configurations.Add(new RegionMap());
            modelBuilder.Configurations.Add(new RoleMap());
            modelBuilder.Configurations.Add(new ServiceMap());
            modelBuilder.Configurations.Add(new ToolMap());
        }
    }
}

In my startup.cs class I'm configuring my ODataModelBuilder in the following way:

ODataModelBuilder builder = new ODataConventionModelBuilder();
            builder.EntitySet<ChangeMeasure>("ChangeMeasure");
            builder.EntitySet<Account>("Account");
            config.MapODataServiceRoute(
                routeName: "ODataRoute",
                routePrefix: null,
                model: builder.GetEdmModel());

One of my model classes "AccountAlia" looks like this:

using System;
using System.Collections.Generic;

namespace CommonDataService.Models
{
    public partial class AccountAlia
    {
        public int AccountAlilasID { get; set; }
        public string AliasName { get; set; }
        public string SourceSystem { get; set; }
        public string SourceColumn { get; set; }
        public string SourceValue { get; set; }
        public Nullable<int> Account_ID { get; set; }
        public virtual Account Account { get; set; }
    }
}

When I attempt to run my program, I get the following error:

An exception of type 'System.InvalidOperationException' 
occurred in System.Web.OData.dll but was not handled in user code

Additional information: The complex type 'CommonDataService.Models.AccountAlia' 
refers to the entity type 'CommonDataService.Models.Account' 
through the property 'Account'.

What is the proper way for me to avoid such conflicts?

Eugene Goldberg
  • 14,286
  • 20
  • 94
  • 167

1 Answers1

1

The error message gives a clue: The complex type 'X' refers to the entity type 'Y'. Currently, OData for Web API does not support references to entity types from within complex types. See The complex type 'MyData.AssetReading' refers to the entity type 'MyData.Asset' through the property 'Asset'.

But it looks to me like you intended AccountAlia to be an entity type. There is a spelling error that is preventing the ODataConventionModelBuilder from recognizing the key property of AccountAlia. Simply rename the property AccountAlilasID to AccountAliaID and the convention-based model builder will recognize AccountAlia as an entity type.

Community
  • 1
  • 1
lencharest
  • 2,825
  • 2
  • 15
  • 22