Asp.net mvc 4 MVC5实体框架数据库优先使用字符串主键
我创建了一个数据库第一实体框架MVC5应用程序。这个表有一个字符串主键。我使用脚手架创建了控制器。我的控制器/索引工作正常,但详细信息、编辑和创建不起作用。如果我使用int主键,所有CRUD都可以正常工作。 EF不支持字符串主键 这是我的Index.cshtmlAsp.net mvc 4 MVC5实体框架数据库优先使用字符串主键,asp.net-mvc-4,frameworks,entity,Asp.net Mvc 4,Frameworks,Entity,我创建了一个数据库第一实体框架MVC5应用程序。这个表有一个字符串主键。我使用脚手架创建了控制器。我的控制器/索引工作正常,但详细信息、编辑和创建不起作用。如果我使用int主键,所有CRUD都可以正常工作。 EF不支持字符串主键 这是我的Index.cshtml @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.Owner) <
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Owner)
</td>
<td>
@Html.DisplayFor(modelItem => item.Address)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Property_Ref }) |
@Html.ActionLink("Details", "Details", new { id=item.Property_Ref }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Property_Ref })
</td>
</tr>
}
所有这些代码首先由MVC5数据库生成,并使用脚手架作为控制器
索引工作正常,列出所有记录,
单击“详细信息”时,在“/”应用程序中出现一个错误。
http://localhost:51356/Property/Details/2
-这里2是一个字符串主键![在此处输入图像描述][1]
非常感谢正如Jhoon Bey所说,在从数据库构建EF数据模型之前,请确保该表已将“Property_Ref”标记为主键。FYR,工作代码: 表格:
private TestDbEntities1 db = new TestDbEntities1();
// GET: /Person/
public ActionResult Index()
{
return View(db.People.ToList());
}
// GET: /Person/Details/5
public ActionResult Details(string id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Person person = db.People.Find(id);
if (person == null)
{
return HttpNotFound();
}
return View(person);
}
// GET: /Person/Create
public ActionResult Create()
{
return View();
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Property_ref }) |
@Html.ActionLink("Details", "Details", new { id=item.Property_ref }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Property_ref })
</td>
</tr>
EF型号:
private TestDbEntities1 db = new TestDbEntities1();
// GET: /Person/
public ActionResult Index()
{
return View(db.People.ToList());
}
// GET: /Person/Details/5
public ActionResult Details(string id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Person person = db.People.Find(id);
if (person == null)
{
return HttpNotFound();
}
return View(person);
}
// GET: /Person/Create
public ActionResult Create()
{
return View();
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Property_ref }) |
@Html.ActionLink("Details", "Details", new { id=item.Property_ref }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Property_ref })
</td>
</tr>
控制器:
private TestDbEntities1 db = new TestDbEntities1();
// GET: /Person/
public ActionResult Index()
{
return View(db.People.ToList());
}
// GET: /Person/Details/5
public ActionResult Details(string id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Person person = db.People.Find(id);
if (person == null)
{
return HttpNotFound();
}
return View(person);
}
// GET: /Person/Create
public ActionResult Create()
{
return View();
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Property_ref }) |
@Html.ActionLink("Details", "Details", new { id=item.Property_ref }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Property_ref })
</td>
</tr>
索引:
private TestDbEntities1 db = new TestDbEntities1();
// GET: /Person/
public ActionResult Index()
{
return View(db.People.ToList());
}
// GET: /Person/Details/5
public ActionResult Details(string id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Person person = db.People.Find(id);
if (person == null)
{
return HttpNotFound();
}
return View(person);
}
// GET: /Person/Create
public ActionResult Create()
{
return View();
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Property_ref }) |
@Html.ActionLink("Details", "Details", new { id=item.Property_ref }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Property_ref })
</td>
</tr>
索引
@ActionLink(“新建”、“创建”)
@DisplayNameFor(model=>model.Name)
@foreach(模型中的var项目){
@DisplayFor(modelItem=>item.Name)
@ActionLink(“编辑”,“编辑”,新的{id=item.Property\u ref})|
@ActionLink(“详细信息”,“详细信息”,新的{id=item.Property\u ref})|
@ActionLink(“删除”,“删除”,新的{id=item.Property\u ref})
}
能否添加更详细的例外情况?这并没有告诉我们任何事情。您是否将“Property\u Ref”设置为数据库中的主键?