1

代码

模型

我有一个基础和一个特定的类

public class Entidade : EntityNome, IAuditavel
{
    public DateTime CriadoAs { get; set; }
    public virtual Usuario CriadoPor { get; set; }
    public DateTime? AtualizadoAs { get; set; }
    public virtual Usuario AtualizadoPor { get; set; }
}

public class Empresa : Entidade
{
    public virtual ICollection<Pessoa> Representantes { get; set; }

    public virtual Pessoa Contato { get; set; }
    public int ContatoId { get; set; }

    public virtual ICollection<Telefone> Telefones { get; set; }
    public virtual ICollection<Endereco> Enderecos { get; set; }
    public virtual ICollection<Email> Emails { get; set; }
}

public class Agencia : Empresa
{
    public string Identificacao { get; set; }
    public virtual Regional Regional { get; set; }
    public int RegionalId { get; set; }
    public virtual ICollection<Pessoa> Gerentes { get; set; }
}

英孚配置

public class EntidadeConfiguracao : EntityTypeConfiguration<Entidade>
{
    public EntidadeConfiguracao()
    {
        Property(p => p.Nome).IsName(true);
        HasRequired(p => p.CriadoPor).WithMany().WillCascadeOnDelete(false);

        ToTable("Entidades");
    }
}

public class EmpresaConfiguracao : EntityTypeConfiguration<Empresa>
{
    public EmpresaConfiguracao()
    {
        HasMany(p => p.Telefones).WithMany().Map(m => m.ToTable("EmpresaTelefones"));
        HasMany(p => p.Emails).WithMany().Map(m => m.ToTable("EmpresaEmails"));
        HasMany(p => p.Enderecos).WithMany().Map(m => m.ToTable("EmpresaEnderecos"));
        HasMany(p => p.Representantes).WithMany().Map(m => m.ToTable("EmpresaRepresentantes"));
        HasRequired(p => p.Contato).WithMany().HasForeignKey(p => p.ContatoId);

        ToTable("Empresas");
    }
}

public class AgenciaConfiguracao : EntityTypeConfiguration<Agencia>
{
    public AgenciaConfiguracao()
    {
        HasRequired(p => p.Regional).WithMany().HasForeignKey(p => p.RegionalId).WillCascadeOnDelete(true);
        Property(p => p.Identificacao).IsRequired().HasMaxLength(10);
        HasMany(p => p.Gerentes).WithMany().Map(m => m.ToTable("AgenciaGerentes"));

        ToTable("Agencias");
    }
}

WebApi 配置

modelBuilder.EntitySet<Entidade>("entidades");
modelBuilder.EntitySet<Empresa>("empresas");
modelBuilder.EntitySet<Agencia>("agencias");

问题

当请求 GET/api/agencias(4471)时返回:

    "internalexception": {
        "message": "Invalid column name 'RegionalId'.\r\nInvalid column name 'ContatoId'.",
        "type": "System.Data.SqlClient.SqlException",
        "stacktrace": "   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)\r\n   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)\r\n   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)\r\n   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)\r\n   at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()\r\n   at System.Data.SqlClient.SqlDataReader.get_MetaData()\r\n   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)\r\n   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite)\r\n   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)\r\n   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)\r\n   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)\r\n   at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)\r\n   at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)\r\n   at System.Data.Entity.Infrastructure.DbCommandDispatcher.<>c__DisplayClassb.<Reader>b__8()\r\n   at System.Data.Entity.Infrastructure.InternalDispatcher`1.Dispatch[TInterceptionContext,TResult](Func`1 operation, TInterceptionContext interceptionContext, Action`1 executing, Action`2 executed)\r\n   at System.Data.Entity.Infrastructure.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext`1 interceptionContext)\r\n   at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior behavior)\r\n   at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)\r\n   at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)"
    }

试图

搜索互联网,我发现可能的原因可能是配置错误,如问题Multiple inheritance with Entity Framework TPC所示。

所以我尝试按照描述进行设置:

我补充Map(m => m.MapInheritedProperties());EmpresaConfiguracaoAgenciaConfiguracao

错误:

在包管理器控制台上运行Add-Migration test或执行应用程序结果:

'Empresa' 类型无法按定义进行映射,因为它映射来自使用实体拆分或其他形式继承的类型的继承属性。要么选择不同的继承映射策略以便不映射继承的属性,要么更改层次结构中的所有类型以映射继承的属性并且不使用拆分。

4

0 回答 0