无法从数据库检索数据->错误->无法在LINQ to Entities查询中构造实体或复杂类型...>

问题描述 投票:0回答:1

我已经检查了几个问题的大多数,但是很遗憾,由于缺乏足够的经验,如果没有您的帮助,将无法解决此问题。

我不明白为什么会发生此错误。我认为代码没有错。

要在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>

错误

    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>

Error Screen

我已经检查了几个问题的大多数,但是很遗憾,由于缺乏足够的经验,没有您的帮助将无法解决此问题。我不明白为什么会发生此错误。...

c# asp.net-mvc ef-code-first visual-studio-debugging
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.