Cra*_*der 7 c# sql entity-framework-core
我有一个由多种方法调用的类。这个类是:
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; }
Run Code Online (Sandbox Code Playgroud)
现在我有以下存储过程:
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
Run Code Online (Sandbox Code Playgroud)
此存储过程返回上面列出的所有字段的数据。但是,当我拨打以下电话时:
public async Task<List<Policy>> GetActivePoliciesByCustomerId(Guide customerId)
{
var activePolicies = await _context.Policy
.FromSqlRaw<Policy>("EXEC [GetActivePoliciesByCustomer] @customerId={0}", customerId)
.ToListAsync();
return activepolicies;
}
Run Code Online (Sandbox Code Playgroud)
在调试会话期间,我看到 Status 和 PolicyLineOfBusiness 设置为 null。我怀疑该[NotMapped]属性正在阻止这些字段被映射,并且我已经验证了这一点。如果删除[NotMapped]属性,我会看到 Status 和 PolicyLineOfBusiness 字段已填充。然而,这个类(Policy)被另一个调用使用:
public async Task<Policy> GetPolicyByPolicyId(Guid id)
{
var policyDetails = await _context.Policy
.FromSqlRaw<Policy>("EXEC [GetPolicyDetailsByPolicyId] @policyId={0}", id)
.ToListAsync();
return policyDetails.FirstOrDefault();
}
Run Code Online (Sandbox Code Playgroud)
它调用的存储过程:
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)
Run Code Online (Sandbox Code Playgroud)
如果我[NotMapped]从上述属性(PolicyLineOfBusiness 和 Status)中删除该属性并使用之前的方法 (GetPolicyByPolicyId) 调用上述存储过程,则会出现异常:
System.InvalidOperationException: The required column 'PolicyLineOfBusiness' was not present in the results of a 'FromSql' operation.
Run Code Online (Sandbox Code Playgroud)
那么如何解决使属性成为可选的问题,同时它们应该能够映射到两个不同存储过程返回的字段?如果有更好的方法,我愿意接受建议。提前致谢。
| 归档时间: |
|
| 查看次数: |
1836 次 |
| 最近记录: |