EF 外键为 NULL
EF foreign key is NULL
我有以下型号class:
[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: 'Object reference not set to an instance of an object.'
有谁知道为什么?即使我尝试将 ?
放在模型上,它也不起作用。
非常感谢。
保留所有现有代码,我为您找到了这个解决方案。只需要改变一下你的观点。控制器没有变化。
型号:
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>
Note: There are few other way to handle this scenario from controller side but this one I think more less code and easiest way. <td>@item.CustomerType?.CustomerTypeCode</td>
this also would work but I prefer to use ternary
operator here
输出:
希望对您有所帮助。
您只需添加,
[必填]
属性
我有以下型号class:
[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: 'Object reference not set to an instance of an object.'
有谁知道为什么?即使我尝试将 ?
放在模型上,它也不起作用。
非常感谢。
保留所有现有代码,我为您找到了这个解决方案。只需要改变一下你的观点。控制器没有变化。
型号:
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>
Note: There are few other way to handle this scenario from controller side but this one I think more less code and easiest way.
<td>@item.CustomerType?.CustomerTypeCode</td>
this also would work but I prefer to useternary
operator here
输出:
希望对您有所帮助。
您只需添加,
[必填]
属性