2016-04-23 2 views
1

Ich erstelle einen selbst gehosteten WebAPI 2-OData 4-Dienst unter Verwendung von Db Context, der aus einer vorhandenen Datenbank umgekehrt wird. Der Kontext sieht wie folgt aus:So konfigurieren Sie ODataModelBuilder ordnungsgemäß, um Konflikte mit virtuellen Eigenschaften von Modellobjekten zu vermeiden

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 meiner startup.cs Klasse meine ODataModelBuilder in der folgenden Art und Weise bin ich zu konfigurieren:

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

Einer meiner Modellklassen "AccountAlia" sieht wie folgt aus:

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; } 
    } 
} 

Wenn ich versuche, mein Programm laufen zu lassen, erhalte ich folgende Fehlermeldung:

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'. 

Was ist der richtige Weg für mich, solche Konflikte zu vermeiden?

Antwort

1

Die Fehlermeldung gibt einen Hinweis: Der komplexen Typ ‚X‘ bezieht sich auf die Entitätstyp ‚Y‘. Derzeit, OData for Web API does not support references to entity types from within complex types. Siehe The complex type 'MyData.AssetReading' refers to the entity type 'MyData.Asset' through the property 'Asset'.

Aber es sieht für mich aus, als ob Sie beabsichtigen, AccountAlia ein Entitätstyp zu sein. Es gibt einen Rechtschreibfehler, der verhindert, dass die ODataConventionModelBuilder die Schlüsseleigenschaft AccountAlia erkennt. Benennen Sie einfach die Eigenschaft in AccountAliaID um, und der konventionsbasierte Modell-Builder erkennt AccountAlia als Entitätstyp.

Verwandte Themen