我正在使用Db Context构建自托管的WebAPI 2 OData 4服务,该服务从现有数据库进行反向工程。 上下文如下所示:
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());
}
}
}
在我的startup.cs类中,我按以下方式配置我的ODataModelBuilder:
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<ChangeMeasure>("ChangeMeasure");
builder.EntitySet<Account>("Account");
config.MapODataServiceRoute(
routeName: "ODataRoute",
routePrefix: null,
model: builder.GetEdmModel());
我的一个模特课&#34; AccountAlia&#34;看起来像这样:
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; }
}
}
当我尝试运行程序时,出现以下错误:
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'.
对我来说,避免此类冲突的正确方法是什么?
答案 0 :(得分:1)
错误消息提供了线索:复杂类型'X'指的是实体类型'Y'。目前,OData for Web API does not support references to entity types from within complex types。请参阅The complex type 'MyData.AssetReading' refers to the entity type 'MyData.Asset' through the property 'Asset'。
但我认为你希望AccountAlia
成为实体类型。有一个拼写错误阻止ODataConventionModelBuilder
识别AccountAlia
的关键属性。只需将属性AccountAlilasID
重命名为AccountAliaID
,基于约定的模型构建器就会将AccountAlia
识别为实体类型。