无法从数据库中检索数据 -> 错误 -> 实体或复杂类型...无法在 LINQ to Entities 查询中构造
Can not retrieve data from DB -> ERROR - >The entity or complex type ... cannot be constructed in a LINQ to Entities query
我已经检查了几个问题的大部分,但不幸的是,由于缺乏足够的经验,没有你的帮助将无法解决这个问题。
我不明白为什么会出现这个错误。我认为代码没有错。
查看完整代码 github
这是
UserController
using BookRental.Models;
using BookRental.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
namespace BookRental.Controllers
{
public class UserController : Controller
{
private ApplicationDbContext db;
public UserController()
{
db = ApplicationDbContext.Create();
}
// GET: User/Index
public ActionResult Index()
{
var userList = (from u in db.Users
join m in db.MembershipTypes on u.membershipTypeId equals m.membershipTypesIdPK
select new UserViewModel
{
Id = u.Id,
fname = u.fname,
lname = u.lname,
email = u.Email,
phone = u.phone,
bdate = u.bdate,
userMemTypeId = u.membershipTypeId,
MembershipTypes = (ICollection<MembershipTypes>)db.MembershipTypes.ToList().Where(n => n.membershipTypesIdPK.Equals(u.membershipTypeId)),
disabled = u.disabled
}).ToList();
return View(userList);
}
UserViewModel
using BookRental.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace BookRental.ViewModel
{
public class UserViewModel
{
[Required]
public string Id { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
public string email { get; set; }
[DataType(DataType.Password)]
public string password { get; set; }
[DataType(DataType.Password)]
public string confirmPassword { get; set; }
public ICollection<MembershipTypes> MembershipTypes { get; set; }
[Required]
public int userMemTypeId { get; set; }
[Required]
public string fname { get; set; }
[Required]
public string lname { get; set; }
[Required]
public string phone { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM dd yyyy}")]
public DateTime bdate { get; set; }
public bool disabled { get; set; }
}
}
索引视图
@model IEnumerable<BookRental.ViewModel.UserViewModel>
@using BookRental.Models
@{
ViewBag.Title = "Index";
}
<h2>Genre</h2>
@Html.Partial("_CreateButtonPartial")
<br />
<br />
<br />
<table class="table table-hover">
<thead class="thead-dark">
<tr class="row">
<th class="col">
@Html.DisplayNameFor(m => m.fname)
</th>
<th class="col">
@Html.DisplayNameFor(m => m.lname)
</th>
<th class="col">
@Html.DisplayNameFor(m => m.email)
</th>
<th class="col">
@Html.DisplayNameFor(m => m.bdate)
</th>
<th class="col">
@Html.DisplayNameFor(m => m.phone)
</th>
<th class="col">
@Html.DisplayNameFor(m => m.disabled)
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr class="row">
<td class="col">
@Html.DisplayNameFor(item => item.fname)
</td>
<td class="col">
@Html.DisplayNameFor(item => item.lname)
</td>
<td class="col">
@Html.DisplayNameFor(item => item.email)
</td>
<td class="col">
@Html.DisplayNameFor(item => item.bdate)
</td>
<td class="col">
@Html.DisplayNameFor(item => item.phone)
</td>
<td class="col">
@Html.CheckBoxFor(m => item.disabled, new { @class = "disabled" })
</td>
<td class="col">
@Html.Partial("_TableButtonPartial", new IndividualButtonPartial { userId = item.Id})
</td>
</tr>
}
</tbody>
</table>
Error
System.NotSupportedException
HResult=0x80131515
Message=The entity or complex type 'BookRental.Models.UserViewModel' cannot be constructed in a LINQ to Entities query.
Source=<Cannot evaluate the exception source>
StackTrace:
<Cannot evaluate the exception stack trace>
问题在这里得到解释; The entity cannot be constructed in a LINQ to Entities query
If you project onto a mapped entity, what you basically do is partially load an entity, which is not a valid state. EF won't have any clue how to e.g. handle an update of such an entity in the future (the default behaviour would be probably overwriting the non-loaded fields with nulls or whatever you'll have in your object)
在您的 Identitymodels.cs 中,您将 UserViewModel 定义为 Entity Framework 实体,这是不正确的,因为您已经有一个 ApplicationUsers 实体,并且来自将自己命名为 UsersViewModel,您希望它只是一个视图模型。
要解决此问题,只需从 IdentityModels.cs;
中删除此行
public System.Data.Entity.DbSet<BookRental.ViewModel.UserViewModel> UserViewModels { get; set; }
我已经检查了几个问题的大部分,但不幸的是,由于缺乏足够的经验,没有你的帮助将无法解决这个问题。
我不明白为什么会出现这个错误。我认为代码没有错。
查看完整代码 github
这是
UserController
using BookRental.Models;
using BookRental.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
namespace BookRental.Controllers
{
public class UserController : Controller
{
private ApplicationDbContext db;
public UserController()
{
db = ApplicationDbContext.Create();
}
// GET: User/Index
public ActionResult Index()
{
var userList = (from u in db.Users
join m in db.MembershipTypes on u.membershipTypeId equals m.membershipTypesIdPK
select new UserViewModel
{
Id = u.Id,
fname = u.fname,
lname = u.lname,
email = u.Email,
phone = u.phone,
bdate = u.bdate,
userMemTypeId = u.membershipTypeId,
MembershipTypes = (ICollection<MembershipTypes>)db.MembershipTypes.ToList().Where(n => n.membershipTypesIdPK.Equals(u.membershipTypeId)),
disabled = u.disabled
}).ToList();
return View(userList);
}
UserViewModel
using BookRental.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace BookRental.ViewModel
{
public class UserViewModel
{
[Required]
public string Id { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
public string email { get; set; }
[DataType(DataType.Password)]
public string password { get; set; }
[DataType(DataType.Password)]
public string confirmPassword { get; set; }
public ICollection<MembershipTypes> MembershipTypes { get; set; }
[Required]
public int userMemTypeId { get; set; }
[Required]
public string fname { get; set; }
[Required]
public string lname { get; set; }
[Required]
public string phone { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM dd yyyy}")]
public DateTime bdate { get; set; }
public bool disabled { get; set; }
}
}
索引视图
@model IEnumerable<BookRental.ViewModel.UserViewModel>
@using BookRental.Models
@{
ViewBag.Title = "Index";
}
<h2>Genre</h2>
@Html.Partial("_CreateButtonPartial")
<br />
<br />
<br />
<table class="table table-hover">
<thead class="thead-dark">
<tr class="row">
<th class="col">
@Html.DisplayNameFor(m => m.fname)
</th>
<th class="col">
@Html.DisplayNameFor(m => m.lname)
</th>
<th class="col">
@Html.DisplayNameFor(m => m.email)
</th>
<th class="col">
@Html.DisplayNameFor(m => m.bdate)
</th>
<th class="col">
@Html.DisplayNameFor(m => m.phone)
</th>
<th class="col">
@Html.DisplayNameFor(m => m.disabled)
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr class="row">
<td class="col">
@Html.DisplayNameFor(item => item.fname)
</td>
<td class="col">
@Html.DisplayNameFor(item => item.lname)
</td>
<td class="col">
@Html.DisplayNameFor(item => item.email)
</td>
<td class="col">
@Html.DisplayNameFor(item => item.bdate)
</td>
<td class="col">
@Html.DisplayNameFor(item => item.phone)
</td>
<td class="col">
@Html.CheckBoxFor(m => item.disabled, new { @class = "disabled" })
</td>
<td class="col">
@Html.Partial("_TableButtonPartial", new IndividualButtonPartial { userId = item.Id})
</td>
</tr>
}
</tbody>
</table>
Error
System.NotSupportedException
HResult=0x80131515
Message=The entity or complex type 'BookRental.Models.UserViewModel' cannot be constructed in a LINQ to Entities query.
Source=<Cannot evaluate the exception source>
StackTrace:
<Cannot evaluate the exception stack trace>
问题在这里得到解释; The entity cannot be constructed in a LINQ to Entities query
If you project onto a mapped entity, what you basically do is partially load an entity, which is not a valid state. EF won't have any clue how to e.g. handle an update of such an entity in the future (the default behaviour would be probably overwriting the non-loaded fields with nulls or whatever you'll have in your object)
在您的 Identitymodels.cs 中,您将 UserViewModel 定义为 Entity Framework 实体,这是不正确的,因为您已经有一个 ApplicationUsers 实体,并且来自将自己命名为 UsersViewModel,您希望它只是一个视图模型。
要解决此问题,只需从 IdentityModels.cs;
中删除此行public System.Data.Entity.DbSet<BookRental.ViewModel.UserViewModel> UserViewModels { get; set; }