EF Core使用空引用执行存储过程

时间:2018-02-10 22:44:57

标签: c# ef-core-2.0

执行存储过程会使用EF Core 2.0创建此错误。这个错误让我感到困惑。有什么指针吗?

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class='col-md-6 col-xs-6'>
  <section class='menu-class' data-itemid="111">
    <div class="amount">
      <input type="number" placeholder="Quantity" min="0" name="quant_111">
    </div>
    <div class="item">
      <h1>My Meat :-)</h1>
      <p class="food">Meat</p>
      <p class="price" data-price="1000.0">1,000.00</p>
    </div>
  </section>
  <hr>
  <section class='menu-class' data-itemid="112">
    <div class="amount">
      <input type="number" placeholder="Quantity" min="0" name="quant_112">
    </div>
    <div class="item">
      <h1>My bread :-)</h1>
      <p class="food">Bread</p>
      <p class="price" data-price="2000.0">2,000.00</p>
    </div>
  </section>
</section>
<section class='col-md-6 col-xs-6'>
  <section class='finalprice'>
    <h1>Final price</h1>
    <p>
    </p>
  </section>
</section>

存储过程的模型类。

这是我的实现类

public class SalesDTO :  BaseEntity
{
    public int SalesID { get; set; }
    [Display(Name = "User")]
    public string SalesRep { get; set; }
    [Display(Name = "Sales Date")]
    public DateTime OrderDate { get; set; }
    public string CustomerName { get; set; }
    public int? CustomerID { get; set; }
    [Display(Name = "Sale Center")]
    public string CenterName { get; set; }
    public int CenterID { get; set; }
    public decimal TotalAmount { get; set; }
    public SalesType SalesType { get; set; }
}

此代码抛出:

  

NullReferenceException:未将对象引用设置为对象的实例。

使用此堆栈跟踪

Stack Trace

但是,如果我使用SQL Server分析器,我可以看到当我将执行的内容复制回SQL Server Management Studio并执行它时,存储过程成功执行。

2 个答案:

答案 0 :(得分:0)

请试试下面的代码。

return DataContext.Set<SalesDTO>().FromSql($"EXECUTE spSalesList {centerID} {salesRep}")
             .Select(x => new SalesDTO
             {
                 Id = x.SalesID,
                 SalesID = x.SalesID,
                 CustomerID = x.CustomerID,
                 CustomerName = x.CustomerName,
                 SalesType = x.SalesType,
                 TotalAmount = x.TotalAmount,
                 SalesRep = x.SalesRep,
                 CenterName = x.CenterName,
                 OrderDate = x.OrderDate,
                 CenterID = x.CenterID
             }).AsNoTracking().ToList();

同时,了解其局限性是值得的: https://docs.microsoft.com/en-us/ef/core/querying/raw-sql#limitations

答案 1 :(得分:0)

我刚刚意识到存储库工作正常,它只是数据解析。

如果在.Select中使用一个属性来匹配2个不同的属性,则会生成NULL Reference错误。 所以这个

return DataContext.Set<SalesDTO>().FromSql("spSalesList @CenterID, @SalesRep, @DateFrom, @DateTo", parms.ToArray())
                 .Select(x => new SalesDTO
                 {
                     Id = x.SalesID,
                     SalesID = x.SalesID,
                     CustomerID = x.CustomerID,
                     CustomerName = x.CustomerName,
                     SalesType = x.SalesType,
                     TotalAmount = x.TotalAmount,
                     SalesRep = x.SalesRep,
                     CenterName = x.CenterName,
                     OrderDate = x.OrderDate,
                     CenterID = x.CenterID
                 }).AsNoTracking().ToList();

将给出Null Reference错误,因为SalesID与SalesID列和Id列都匹配,尽管它可能是同一个字段。 但是,当您将代码更改为

return DataContext.Set<SalesDTO>().FromSql("spSalesList @CenterID, @SalesRep, @DateFrom, @DateTo", parms.ToArray())
                 .Select(x => new SalesDTO
                 {
                     Id = x.Id,
                     SalesID = x.SalesID,
                     CustomerID = x.CustomerID,
                     CustomerName = x.CustomerName,
                     SalesType = x.SalesType,
                     TotalAmount = x.TotalAmount,
                     SalesRep = x.SalesRep,
                     CenterName = x.CenterName,
                     OrderDate = x.OrderDate,
                     CenterID = x.CenterID
                 }).AsNoTracking().ToList();

然后它正常工作,因为Id和SalesID匹配到不同的字段。我还不明白为什么会这样。 当我理解时,我会添加评论。