【问题标题】:EF foreign key is NULLEF 外键为 NULL
【发布时间】:2021-10-28 21:50:28
【问题描述】:

我有以下模型类:

[key]
public int CustomerId { get; set; }
public string CustomerName { get; set; }
[ForeignKey("CustomerType")]
public int? CustomerTypeId { get; set; }
public virtual CustomerType CustomerType { get; set; }

[Key]
public int CustomerTypeId { get; set; }
public string CustomerTypeCode { get; set; }

然后我想用这个控制器代码将它加载到一个视图中:

public async Task<IActionResult> Index()
{
     var obj = _db.Customer.Include(i => i.CustomerType);
     return View(await obj.ToListAsync());
}

在观点上:

@using IEnumerable<myProject.Models.Customer>
<table>
<thead>
   <tr>
     <th>Customer
     <th>CustomerType
   </tr>
</thead>
<tbody>
   @foreach (var item in Model)
   {
   <tr>
     <td>@item.Customer</td>
     <td>@item.CustomerType.Code</td>
   </tr>
   }
</tbody>
</table>

当我尝试运行它时,我收到@item.CustomerType.Code 的错误,因为数据库中的数据为空:

System.NullReferenceException: '对象引用未设置为对象的实例。'

有人知道为什么吗?即使我尝试将? 放在模型上,它也不起作用。

非常感谢。

【问题讨论】:

  • 根据您的代码,来自Customer 的一些记录可能没有CustomerType。这就是你的情况:item.CustomerTypenull。添加检查 null 或定义 public int CustomerTypeId { get; set; } 取决于您的数据设计。
  • 那么您对空数据的期望处理是什么,或者不将空数据绑定到表中?你想要哪一个?
  • @Jackdaw 是的,有些客户将没有 CustomerType。我在模型中输入了?。但同样,它出现了错误。我该如何处理?
  • @MdFaridUddinKiron,我想处理空数据。所以它会在视图上空白。
  • 谢谢你的回复很酷,请稍等

标签: c# entity-framework asp.net-core razor-pages


【解决方案1】:

您只需添加,

[必填]

房产

【讨论】:

    【解决方案2】:

    保留所有现有代码,我为您找到了这个解决方案。只需要改变你的观点。控制器没有变化。

    型号:

    public class Customer
        {
            [key]
            public int CustomerId { get; set; }
            public string CustomerName { get; set; }
            [ForeignKey("CustomerType")]
            public int? CustomerTypeId { get; set; }
            public virtual CustomerType CustomerType { get; set; }
        }
    
    public class CustomerType
        {
            [Key]
            public int CustomerTypeId { get; set; }
            public string CustomerTypeCode { get; set; }
          
        }
    

    控制器:

      public async Task<IActionResult> CustomerIndex()
            {
                var obj = _context.Customers.Include(i => i.CustomerType);
                return View(await obj.ToListAsync());
            }
    

    查看:

    @model IEnumerable<MVCApps.Models.Customer>
    <table class="table table table-bordered">
        <thead>
            <tr>
                <th>Customer
                <th>Customer Type
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model)
            {
                <tr>
                    <td>@item.CustomerName</td>
                    <td>@(item.CustomerType == null ? "" : item.CustomerType.CustomerTypeCode)</td>
                </tr>
            }
        </tbody>
    </table>
    

    注意:几乎没有其他方法可以从控制器端处理这种情况,但我认为这是一种更少代码和最简单的方法。 &lt;td&gt;@item.CustomerType?.CustomerTypeCode&lt;/td&gt; 这也可以,但我更喜欢在这里使用ternary 运算符

    输出:

    希望对你有帮助。

    【讨论】:

    • 嗨,非常感谢。但是错误还是一样。
    • 我找到了解决方案。我只需要在上面加上?...@item.CustomerType?.CustomerTypeCode .... 就可以了。
    • 我的解决方案应该可以正常工作,只有模型上的? 不能正常工作,您必须检查空值。 &lt;td&gt;@(item.CustomerType == null ? "" : item.CustomerType.CustomerTypeCode)&lt;/td&gt; 就是这样,它不应该出错,我加倍你正确放置它。查看完整的更新答案。
    • 你的答案是正确的!完美的!非常感谢。真的很感激。
    • 此外,@item.CustomerType?.CustomerTypeCode 视图上的这个也应该可以工作,但我更喜欢使用 tarnary 运算符。
    猜你喜欢
    • 1970-01-01
    • 2018-11-30
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 2021-03-20
    • 2017-02-23
    • 1970-01-01
    • 2015-06-03
    相关资源
    最近更新 更多