C# 如何正确配置ODataModelBuilder以避免与模型对象的虚拟属性冲突
我正在使用Db上下文构建一个自托管的WebAPI 2 OData 4服务,它是从现有数据库反向工程的。 上下文如下所示:C# 如何正确配置ODataModelBuilder以避免与模型对象的虚拟属性冲突,c#,asp.net-web-api,odata,C#,Asp.net Web Api,Odata,我正在使用Db上下文构建一个自托管的WebAPI 2 OData 4服务,它是从现有数据库反向工程的。 上下文如下所示: using System.Data.Entity; using System.Data.Entity.Infrastructure; using CommonDataService.Models.Mapping; namespace CommonDataService.Models { public partial class MALContext : DbCont
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());
}
}
}
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; }
}
}
对于我来说,避免此类冲突的正确方法是什么?错误消息给出了一条线索:复杂类型'X'指的是实体类型'Y'。目前。看 但在我看来,您希望
AccountAlia
成为一种实体类型。有一个拼写错误阻止了ODataConventionModelBuilder
识别AccountAlia
的关键属性。只需将属性AccountAlilasID
重命名为AccountAliaID
,基于约定的模型生成器就会将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'.