我想了解下面的代码,但找不到有关EntityReference& amp;互联网上的EntityKey。下面的代码做了什么?
编辑:我意识到这是与外键相关的东西,我正在进一步调查。
Employee e = db.Employee.FirstOrDefault((Employee x) => x.Reference == data.EmployeeReference);
e.LocationReference.EntityKey = new EntityKey("FlexibleBenefitsDb.Location", "ID", xpatLocationID);
和
public EntityReference<Location> LocationReference
{
get
{
return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<Location>("Nestle.FlexibleBenefits.Model.EmployeeLocation", "Location");
}
set
{
if (value != null)
{
((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedReference<Location>("Nestle.FlexibleBenefits.Model.EmployeeLocation", "Location", value);
}
}
}
有关完整的源代码,请查看以下内容:
完整代码:
using Nestle.FlexibleBenefits.Business;
using Nestle.FlexibleBenefits.Business.BenefitCostCalculations;
using Nestle.FlexibleBenefits.DataAccess;
using Nestle.FlexibleBenefits.DataImport.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Transactions;
namespace Nestle.FlexibleBenefits.DataImport
{
public class FlexibleBenefitDbImporter : IBenefitImporter
{
public void Import(IBenefitLoader loader)
{
List<RawEmployeeBenefit> dataList = loader.Load();
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, new TimeSpan(0, 30, 0)))
{
using (FlexibleBenefitsDb db = new FlexibleBenefitsDb())
{
foreach (RawEmployeeBenefit data in dataList)
{
Employee e = db.Employee.FirstOrDefault((Employee x) => x.Reference == data.EmployeeReference);
if (e == null)
{
e = new Employee();
e.Reference = data.EmployeeReference;
}
e.DailyGrossSalary = data.DailyGrossSalary;
RawEmployeeBenefitDetail xpatCOR = data.Details.FirstOrDefault((RawEmployeeBenefitDetail x) => x.BenefitCode == "COR001" && x.Value == string.Empty);
RawEmployeeBenefitDetail xpatCOR2 = data.Details.FirstOrDefault((RawEmployeeBenefitDetail x) => x.BenefitCode == "COR002" && x.Value == string.Empty);
if (xpatCOR != null && xpatCOR2 != null)
{
int xpatLocationID = 0;
if (data.LocationID == 2)
{
xpatLocationID = 4;
}
else
{
if (data.LocationID == 3)
{
xpatLocationID = 5;
}
}
e.LocationReference.EntityKey = new EntityKey("FlexibleBenefitsDb.Location", "ID", xpatLocationID);
}
else
{
e.LocationReference.EntityKey = new EntityKey("FlexibleBenefitsDb.Location", "ID", data.LocationID);
}
e.FirstName = data.FirstName;
if (data.IncomeTaxRatio > 1m)
{
data.IncomeTaxRatio /= 100m;
}
e.IncomeTaxRatio = data.IncomeTaxRatio;
e.LastName = data.LastName;
e.UserName = string.Format("{0}\\{1}", SystemProperties.DomainName, data.UserName);
e.MaritalStatusReference.EntityKey = new EntityKey("FlexibleBenefitsDb.MaritalStatus", "ID", data.MaritalStatusID);
e.MaxExchangeableLeaveDays = data.MaxExchangeableLeaveDays;
PrivatePensionFund privatePensionFund = db.PrivatePensionFund.FirstOrDefault((PrivatePensionFund x) => x.MaritalStatus.ID == data.MaritalStatusID && x.GrossSalaryRangeStart < e.DailyGrossSalary * 30m && x.GrossSalaryRangeEnd > e.DailyGrossSalary * 30m);
if (privatePensionFund == null)
{
e.MaxPrivatePensionFund = 0m;
}
else
{
e.MaxPrivatePensionFund = privatePensionFund.MaxPrivatePensionFund;
}
EmployeeBenefit eb = new EmployeeBenefit();
eb.Employee = e;
eb.BenefitAllowanceGrossTotal = 0m;
eb.BenefitYear = SystemProperties.ActiveTargetYear;
eb.CoreBenefitGrossTotal = 0m;
eb.IsApproved = false;
eb.IsModified = false;
eb.StoredFileReference.EntityKey = new EntityKey("FlexibleBenefitsDb.StoredFile", "ID", loader.StoredFileID);
foreach (RawEmployeeBenefitDetail detailData in data.Details)
{
if (!string.IsNullOrEmpty(detailData.Value))
{
EmployeeBenefitDetail ebd = new EmployeeBenefitDetail();
Benefit b = db.Benefit.Include("BenefitType").FirstOrDefault((Benefit bnf) => bnf.Code == detailData.BenefitCode);
if (b == null)
{
throw new ApplicationException(string.Format("Invalid benefit code {0} specified", detailData.BenefitCode));
}
if (b.BenefitType.ID != 1 && b.BenefitType.ID != 2)
{
throw new ApplicationException("Only Core and Exchangable benefits are imported. Wrong BenefitCode.");
}
ebd.Benefit = b;
ebd.EmployeeBenefit = eb;
ebd.IsExchanged = false;
ebd.Quantity = 0m;
ebd.NetCost = decimal.Parse((detailData.Value == string.Empty) ? "0" : detailData.Value);
if (b.ApplyIncomeTax || b.ApplySocialSecurityTax || b.ApplyStampTax)
{
ebd.GrossCost = NetGrossCalculations.ConvertNetToGross(e, ebd.NetCost, b.ApplyStampTax, b.ApplySocialSecurityTax, b.ApplyStampTax);
}
else
{
ebd.GrossCost = ebd.NetCost;
}
eb.BenefitAllowanceGrossTotal += ebd.GrossCost;
if (b.BenefitType.ID == 1)
{
eb.CoreBenefitGrossTotal += ebd.GrossCost;
}
eb.EmployeeBenefitDetails.Add(ebd);
}
}
}
db.SaveChanges();
scope.Complete();
}
}
}
}
}