【问题标题】:how to properly configure ODataModelBuilder to avoid conflict with virtual properties of model objects如何正确配置 ODataModelBuilder 以避免与模型对象的虚拟属性冲突
【发布时间】:2016-04-23 13:39:06
【问题描述】:

我正在使用 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());

我的一个模型类“AccountAlia”如下所示:

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

什么是我避免此类冲突的正确方法?

【问题讨论】:

    标签: c# asp.net-web-api odata


    【解决方案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 识别为实体类型。

    【讨论】:

      猜你喜欢
      • 2014-06-22
      • 2021-08-09
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 2022-07-28
      • 2023-04-03
      • 1970-01-01
      相关资源
      最近更新 更多