Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 映射实体框架中相同数据库列的2个属性_C#_.net_Asp.net Mvc 2_Ef Code First_Entity Framework 4.1 - Fatal编程技术网

C# 映射实体框架中相同数据库列的2个属性

C# 映射实体框架中相同数据库列的2个属性,c#,.net,asp.net-mvc-2,ef-code-first,entity-framework-4.1,C#,.net,Asp.net Mvc 2,Ef Code First,Entity Framework 4.1,我正在使用MVC和c进行EF4.1代码优先开发 这是我的要求: 有没有办法为同一数据库列映射模型类的两个属性 因为我有数据检索问题如下 $.ajax({ type: "GET", url: "/Portal/GetServiceAndRetailSalesDetails", dataType: 'json', contentType: "application/json; charset=UTF-8", data: { invoiceOrSale

我正在使用MVC和c进行EF4.1代码优先开发

这是我的要求:

  • 有没有办法为同一数据库列映射模型类的两个属性
  • 因为我有数据检索问题如下

    $.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);
             }
             });
    
    具有某些复杂计算的db列的当前映射属性

    因此,当我以ajax调用的形式从我的UI调用它时,它将计算值检索为0.0,这是由于计算延迟(当我进行调试,然后降低它将正确生成值的速度时)

    如果我设置了另一个没有任何计算的字段,它将正确显示

    如何克服上述问题

    我的部分型号代码如下。

    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);
    }
    
    public ActionResult GetServiceandDetailsDetails(Guid invoiceOrSaleId、字符串提供程序键、日期时间签出日期、双重签出时间)
    {
    var items=新列表();
    TimeSpan timeResult=TimeSpan.FromHours(签出时间);
    var checkOut=签出日期+时间结果;
    var serviceDetails=Repository.GetAllPayableItems(checkOut,invoiceOrSaleId).ToList();
    foreach(serviceDetails中的var s)
    {
    变量项=新的CheckOutServiceAndRetailItem
    {
    AllocationOrInvoiceOrSaleId=s.Allocation.AllocationId,
    Name=s.Allocation.Service.Name,
    价格=最新总价,
    //价格=总价,
    Class=s.Allocation.Service.IsAnExtra?“RetailOrExtraService”:“,
    };
    项目。添加(项目);
    }
    返回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);
             }
             });
    
    $.ajax({
    键入:“获取”,
    url:“/Portal/getserviceandetailsalesDetails”,
    数据类型:“json”,
    contentType:“应用程序/json;字符集=UTF-8”,
    数据:{invoiceOrSaleId:invoiceOrSaleId,providerKey:providerKey,checkoutDate:checkoutDate,checkoutTime:checkoutTime},
    成功:功能(响应){
    make=“”;
    var totalToBepaid=0.0;
    toBePaidItems=[];
    $。每个(响应、功能(索引、sr){
    如果(未付){
    make++=''++'+sr.Name++'$'+sr.Price.toFixed(2)+';
    }否则{
    make++=''++'+sr.Name++'$'+sr.Price.toFixed(2)+';
    }
    totalToBepaid+=高级价格;
    //插入到数组中
    toBePaidItems.push(sr.allocationorVoiceorSaleid);
    });
    var lateFee=parseFloat($(“#hdnLateFee”).val();
    如果(滞纳金>0){
    totalToBepaid+=滞纳金;
    make+=“迟收费用”+“$”+迟收费用+”;
    }
    使+=”;
    $(#serviceandetaildetails”).html(make);
    }
    });
    
    UI图像如下所示(当我放置制动点并运行时,会显示正确的值)


    我不理解这个问题。
    LatestTotal
    计算的时间和地点。EF只是从数据库或缓存加载当前值。那么问题出在哪里?@LadislavMrnka实际上,当我调用我的存储库方法时,它映射到这个模型,并对“LatestTotal”属性进行必要的计算。但是由于计算的延迟,它返回0.0。当我放入debug和check时,它会正确返回。你知道吗?那么计算是并发的?在这种情况下,AJAX调用必须等待计算完成。我仍然看不到和映射问题的联系。如果计算是在存储库中完成的,那么将两个属性映射到同一列(这是不允许的)会有什么帮助?@LadislavMrnka我如何让Ajax调用等待计算完成?只需在完成之前不从操作返回即可。我看不到您的代码=我不知道您当前是如何实现该调用的。