【发布时间】:2021-11-16 07:19:57
【问题描述】:
我的前任为 HR 编写了一个 Web 应用程序,用于从我们的 HRMS 中提取关于 401k 贡献的数据。事实证明,他的报告仅在准确的美元金额而不是百分比时才提取追赶捐款。他使用实体框架来做到这一点。我是脚本编写者,而不是程序员。我知道阿多。我不知道实体框架。
数据库有一个名为EARNED 的列。 Catch Up Contribution Amounts 获得代码 D03。 Catch Up Contribution Percentage 获得代码 D03B。此列还有其他几个可能的值:D04、D31、E40 等。不知何故,他的报告只提取了 D03 行。它还需要拉取 D03B 行。
如果我正在编写 SQL,它会是这样的
SELECT *
FROM [SageHRMS_900].[dbo].[UPCHKD]
WHERE [EARNDED] = 'D03B'
OR [EARNDED] = 'D03'
显然 Entity Framework 没有使用类似的东西。相反,我看到的只是这样的东西
hrmsEntity hrms = new hrmsEntity(dbConnection);
var detail = hrms.TRP_ConDetail.ToList();
foreach (var line in hrms.TRP_ConDetail.ToList())
{
fileData += line.Plan + line.RecordID + line.Date + line.SSN + line.Fund + line.LoanNum +
line.Amount + line.Salary + line.SalaryType + line.ContType + line.StateTax +
line.LoanPayType + line.Filler01 + Environment.NewLine;
}
我看到了 TRP_Con 类的定义位置
public class TRP_ConDetail
{
[Key]
public Guid ID { get; set; }
public string Plan { get; set; }
public string RecordID { get; set; }
public string Date { get; set; }
public string Name { get; set; }
public string SSN { get; set; }
public string Fund { get; set; }
public string Desc { get; set; }
public string LoanNum { get; set; }
public string Amount { get; set; }
public decimal Dollars { get; set; }
public string Salary { get; set; }
public string SalaryType { get; set; }
public string ContType { get; set; }
public string StateTax { get; set; }
public string LoanPayType { get; set; }
public string Filler01 { get; set; }
public int? LineSq { get; set; }
}
我没有看到代码如何知道将选择限制为仅 D03 类型的贡献。
编辑: 添加 hrmsEntity 类
public partial class hrmsEntity : DbContext
{
//public hrmsEntity()
// : base("name=hrmsEntityModelDSM")
//{
//}
/*Base entity connection depends on the entity of the company*/
public hrmsEntity(string dbConnection) : base($"name={dbConnection}") { }
//public hrmsEntity(string connectionString) : base(connectionString) { }
public virtual DbSet<TRP_ConSourceTotal> TRP_ConSourceTotal { get; set; }
public virtual DbSet<TRP_ConDetail> TRP_ConDetail { get; set; }
public virtual DbSet<TRP_MaintFile> TRP_MaintFile { get; set; }
}
UPCHKD 是 900 数据库中的一个表。
@Dai 评论:“另外,请(为了每个人的理智)请使用您的 IDE 的 Refactor Rename 功能将 hrmsEntity 重命名为 HrmsDbContext” 是的...我不是程序员。 :) 我编写 VBA 或 PowerShell 脚本。调整 C# 几乎是我能力的极限。
【问题讨论】:
-
hrmsEtntiy.TRP_ConDetail看起来像DbSet<T>,对吗? -
正确:
public virtual DbSet<TRP_ConDetail> TRP_ConDetail { get; set; } -
在您的 SQL 查询中使用
UPCHKD,是VIEW还是TABLE?它与TRP_ConDetail相比如何?有hrmsEntity的UPDCHKD成员吗? -
另外,请please please(为了大家的理智)please使用你的IDE重构重命名功能,将
hrmsEntity重命名为HrmsDbContext。 -
您无需重新编写任何内容。如果他确实使用了 ef / ef core,那么使用 linq 真的会节省你很多时间。这很容易学习。它的语法有两种形式:一种模仿 SQL,另一种对编码器更友好。
标签: c# entity-framework entity-framework-6