我正在使用MVC 和 c#进行EF 4.1 代码优先开发。
这是我的要求:
- 有什么方法可以将我的模型类的 2 个属性映射到同一个数据库列?
B'cos 我遇到如下数据检索问题。
db 列的当前映射属性具有一些复杂的计算。
因此,当我从我的 UI 作为 ajax 调用调用它时,它会将计算值检索为 0.0,计算延迟的 b'cos(当我进行调试然后减慢它会正确生成值的东西时)。
如果我设置另一个没有任何计算的字段,它会正确显示。
如何克服上述问题?
我的部分模型代码如下。
public class Invoice
{
public Invoice() { Id = Guid.NewGuid(); Created = DateTime.Now; }
public Guid Id { get; set; }
public decimal LatestTotal { get; set; }
[NotMapped]
public decimal Total
{
get
{
return (LatestTotal = this.CalculateTotal());
}
}
}
有问题的财产是LatestTotal
操作方法如下所示
public ActionResult GetServiceAndRetailSalesDetails(Guid invoiceOrSaleId, string providerKey, DateTime checkoutDate, double checkoutTime)
{
var items = new List<CheckOutServiceAndRetailItem>();
TimeSpan timeResult = TimeSpan.FromHours(checkoutTime);
var checkOut = checkoutDate + timeResult;
var serviceDetails = Repository.GetAllPayableItems(checkOut, invoiceOrSaleId).ToList();
foreach (var s in serviceDetails)
{
var item = new CheckOutServiceAndRetailItem
{
AllocationOrInvoiceOrSaleId = s.Allocation.AllocationId,
Name = s.Allocation.Service.Name,
Price = s.LatestTotal,
//Price = s.Total,
Class = s.Allocation.Service.IsAnExtra ? "RetailOrExtraService" : "",
};
items.Add(item);
}
return Json(items, JsonRequestBehavior.AllowGet);
}
来自 UI 的Ajax 调用如下所示。
$.ajax({
type: "GET",
url: "/Portal/GetServiceAndRetailSalesDetails",
dataType: 'json',
contentType: "application/json; charset=UTF-8",
data: { invoiceOrSaleId: invoiceOrSaleId, providerKey: providerKey, checkoutDate: checkoutDate, checkoutTime: checkoutTime },
success: function (response) {
make = "<table id='tblPayment'>";
var totalToBepaid = 0.0;
toBePaidItems = [];
$.each(response, function (index, sr) {
if (unPaid) {
make += "<tr id=" + sr.AllocationOrInvoiceOrSaleId + " class=" + sr.Class + ">" + "<td style='padding-right:100px'>" + sr.Name + "</td><td class='colTotal' style='padding-right:45px'>" + '$ ' + sr.Price.toFixed(2) + "</td><td></tr>";
} else {
make += "<tr id=" + sr.AllocationOrInvoiceOrSaleId + " class=" + sr.Class + ">" + "<td style='padding-right:100px'>" + sr.Name + "</td><td class='colTotal' style='padding-right:45px'>" + '$ ' + sr.Price.toFixed(2) + "</td></tr>";
}
totalToBepaid += sr.Price;
//insert into array
toBePaidItems.push(sr.AllocationOrInvoiceOrSaleId);
});
var lateFee = parseFloat($("#hdnLateFee").val());
if (lateFee > 0) {
totalToBepaid += lateFee;
make += "<tr class='Fees'><td style='padding-right:100px'>Late Pickup Fee</td><td class='colTotal' style='padding-right:45px'>" + '$ ' + lateFee + "</td></tr>";
}
make += "</table>";
$("#serviceAndRetailDetails").html(make);
}
});
UI 图像如下(当我放一个刹车点并运行然后显示正确的值)