Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

399
Visualizações
Stored Procedure returns value for a column but FromSqlRaw returns null for the class property

I have a class that is called by multiple methods. This class is:

public class Policy : EntityObject
{
    public Guid PolicyId { get; set; }
    public Guid CustomerId { get; set; }
    public string PolicyNumber { get; set; }
    [NotMapped]
    public string TrimmedPolicyNumber { get; set; }
    public DateTime? PolicyEffectiveDate { get; set; }
    public DateTime? PolicyExpirationDate { get; set; }
    [NotMapped]
    public string PolicyType { get; set; }
    public string InsuranceCompany { get; set; }
    public string WritingCompany { get; set; }
    [NotMapped]
    public string BillMethod_PaymentPlan { get; set; }
    [NotMapped]
    public decimal? FullTermPremium { get; set; }
    [NotMapped]
    public string AccountExecutive { get; set; }
    [NotMapped]
    public string AccountRepresentative { get; set; }

    [NotMapped]
    public string PolicyLineOfBusiness { get; set; }

    [NotMapped]
    public string Status { get; set; }

Now I have the following stored proc:

Create PROCEDURE [GetActivePoliciesByCustomer]
@CustomerId UNIQUEIDENTIFIER
AS
    SET NOCOUNT ON;
    Select
        c.CustId as CustomerId
        , p.PolId as PolicyId
        , p.PolNo as PolicyNumber
        , p.PolTypeLOB as [PolicyLineOfBusiness]
        , p.PolEffDate as PolicyEffectiveDate
        , p.PolExpDate as PolicyExpirationDate
        , cmp.Name as InsuranceCompany
        , wcmp.Name as WritingCompany
        , pr.Description as [Status]
    FROM
        Policies p
        (Further details have been omitted as it is not important)
GO

This stored proc returns data for all the fields listed above. However, when I make the following call:

public async Task<List<Policy>> GetActivePoliciesByCustomerId(Guide customerId)
{
    var activePolicies = await _context.Policy
        .FromSqlRaw<Policy>("EXEC [GetActivePoliciesByCustomer] @customerId={0}", customerId)
        .ToListAsync();

    return activepolicies;
}

During the debug session, I see that Status and PolicyLineOfBusiness are set to null. My suspicion is that the [NotMapped] attribute is preventing these fields from getting mapped and I have validated that. If I remove [NotMapped] attribute, I see that the Status and PolicyLineOfBusiness fields are populated. However, this class (Policy) is used by another call:

public async Task<Policy> GetPolicyByPolicyId(Guid id)
{
    var policyDetails = await _context.Policy
        .FromSqlRaw<Policy>("EXEC [GetPolicyDetailsByPolicyId] @policyId={0}", id)
        .ToListAsync();

    return policyDetails.FirstOrDefault();
}

The stored proc it is calling:

CREATE PROCEDURE [GetPolicyDetailsByPolicyId]
@PolicyId UNIQUEIDENTIFIER
AS
    SET NOCOUNT ON;

    SELECT TOP 1
        p.PolId as PolicyId
        , p.CustId as CustomerId
        , p.PolNo as PolicyNumber
        , p.ShortPolNo as TrimmedPolicyNumber
        , p.PolEffDate as PolicyEffectiveDate
        , p.PolExpDate as PolicyExpirationDate
        , p.PolTypeLOB as PolicyType
        , c.[Name] as InsuranceCompany
        , wc.[Name] as WritingCompany
        , p.BillMethod_PaymentPlan
        , p.FullTermPremium
        , e1.[LastName] as AccountExecutive
        , CONCAT(e.FirstName, ' ', e.LastName) as AccountRepresentative 
    From
        Policies p
        (Further details have been omitted as it is not important)

If I remove the [NotMapped] attribute from the aforementioned properties (PolicyLineOfBusiness and Status) and use the previous method (GetPolicyByPolicyId) to call the above stored proc, I get an exception:

System.InvalidOperationException: The required column 'PolicyLineOfBusiness' was not present in the results of a 'FromSql' operation.

So how do I solve the problem of making attributes optional and at the same time, they should be able to map to the fields returned by two different stored procs? If there's a better way, I am open to suggestions. Thanks in advance.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

As PolicyLineOfBusiness property is present at your domain model. So the both SP must return that property value if you remove the attribute(NotMapped). But by removing that attribute your second SP will give an exception and the first SP will get data. So it means that the second SP does not want to return PolicyLineOfBusiness property.

Unfortunately, you can not do that as you write PolicyLineOfBusiness property mandatory by removing the attribute(NotMapped) at the domain Model property. So you have to return the property from the second SP if you want to keep the domain model the same.

CREATE PROCEDURE [GetPolicyDetailsByPolicyId]
@PolicyId UNIQUEIDENTIFIER
AS
    SET NOCOUNT ON;

    SELECT TOP 1
        p.PolId as PolicyId
        , p.CustId as CustomerId
        , p.PolNo as PolicyNumber
        , p.ShortPolNo as TrimmedPolicyNumber
        , p.PolEffDate as PolicyEffectiveDate
        , p.PolExpDate as PolicyExpirationDate
        , p.PolTypeLOB as PolicyType
        , c.[Name] as InsuranceCompany
        , wc.[Name] as WritingCompany
        , p.BillMethod_PaymentPlan
        , p.FullTermPremium
        , "" As PolicyLineOfBusiness --Have to add this as the domain model contain this property
        , e1.[LastName] as AccountExecutive
        , CONCAT(e.FirstName, ' ', e.LastName) as AccountRepresentative 
    From
        Policies p
        (Further details have been omitted as it is not important)

If you do not want to do this then create 2 different models for 2 SPs.

over 4 years ago · Santiago Trujillo Relatório

0

For the separate stored procedure you will need to have separate models as the column are different in both the stored procedures.

over 4 years ago · Santiago Trujillo Relatório

0

Your Policies class you have both PolicyType and PolicyLineOfBusiness properties.

in GetActivePoliciesByCustomer procedure the column p.PolTypeLOB is aliased as PolicyLineOfBusiness but in GetPolicyDetailsByPolicyId procedure the column p.PolTypeLOB is aliased as PolicyType

So, the origin column is the same for both properties, I'm not understanding the reason, but to avoid the exception on 2nd procedure you can modify it adding also the required output for PolicyLineOfBusiness :

CREATE PROCEDURE [GetPolicyDetailsByPolicyId]
@PolicyId UNIQUEIDENTIFIER
AS
    SET NOCOUNT ON;

    SELECT TOP 1
        p.PolId as PolicyId
        , p.CustId as CustomerId
        , p.PolNo as PolicyNumber
        , p.ShortPolNo as TrimmedPolicyNumber
        , p.PolEffDate as PolicyEffectiveDate
        , p.PolExpDate as PolicyExpirationDate
        , p.PolTypeLOB as PolicyType
        , p.PolTypeLOB as PolicyLineOfBusiness /* *** ADD THIS LINE  *** */
        , c.[Name] as InsuranceCompany
        , wc.[Name] as WritingCompany
        , p.BillMethod_PaymentPlan
        , p.FullTermPremium
        , e1.[LastName] as AccountExecutive
        , CONCAT(e.FirstName, ' ', e.LastName) as AccountRepresentative 
    From
        Policies p
        (Further details have been omitted as it is not important)
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda