我使用Visual Studio 2015,.Net framework 4.6.00079构建一个简单的ASP.NET MVC 6应用程序: 模型:
namespace RimconPensionModel.DataAccessLayer
{
[Table("BaseMortality")]
public class BaseMortality
{
[Key]
public int Id { get; set; }
public string BaseMortalitySet { get; set; }
public int Age { get; set; }
public string Gender { get; set; }
public float MortalityFactor { get; set; }
public DateTime EffectiveDate { get; set; }
public DateTime UpdateDate { get; set; }
public string UpdateUser { get; set; }
}
}
控制器:
namespace RimconPensionModel.Controllers
{
public class BaseMortalityController : Controller
{
[FromServices]
public RimconPensionModelContext RimconPensionModelContext { get; set; }
[FromServices]
public ILogger<BaseMortalityController> Logger { get; set; }
public IActionResult Index()
{
return View(RimconPensionModelContext.BaseMortality.ToList());
}
}
}
视图:
@model IEnumerable<RimconPensionModel.DataAccessLayer.BaseMortality>
@{
ViewBag.Title = "Base Mortality Data";
}
<h2>Base Mortality Data</h2>
<!--
<p>
<a asp-controller="BaseMortalityData" asp-action="Create">Create New</a>
</p> -->
<table class="table">
<tr>
<th>Base Mortality Sets</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.BaseMortalitySet)
</td>
</tr>
}
</table>
应用程序构建并启动,但是当我调用控制器时,我在&#34;返回视图(...)&#34;上得到InvalidCastException。控制器的一行。我已经谷歌搜索了几天(我对MVC和实体框架有点新鲜)没有运气。我认为它可能与datetime属性有关。我从csv文件批量加载数据库表,日期时间字段为YYYYMMDD格式,加载正常; db表显示日期时间字段中的1994-12-31 12:00:00 AM。它们的类型为datetime2(7),在db中为NOT NULL。