类型' System.NotSupportedException'的例外情况发生在EntityFramework.SqlServer.dll中但未在用户代码

时间:2018-02-25 13:00:01

标签: c# asp.net-mvc entity-framework

我对这一切都很陌生。

正如标题所示,我收到以下错误:

  

类型' System.NotSupportedException'的例外情况发生在EntityFramework.SqlServer.dll中但未在用户代码中处理

我要做的就是在索引视图中查看人员列表。

我有一个名为staffrep的模型,代码如下:

 using System.Collections.Generic;
 using System.Linq;
 using BookingsModel;

 namespace Bookings.Models
{
    public class staffrep
    {
        private ModelBookings db = new ModelBookings();

        public IEnumerable<BookingsModel.Staff> All()
        {
        return db.Staffs.Where(b => b.Active.Equals(true));
        }
    }
} 

然后我有员工控制器,代码如下(我可以查看工作人员,如果我使用两行注释掉)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using BookingsModel;

namespace Bookings.Controllers
{
    public class StaffController : Controller
    {
        BookingsModel.ModelBookings db = new BookingsModel.ModelBookings();
        // GET: Staff
        public ActionResult Index()
        {
            Models.staffrep staffrep = new Models.staffrep();
            var staff_ = staffrep.All();
            return View(staff_);

            //var staff_ = db.Staffs.Where(b => b.Active == true);
            //return View(staff_);

        }

我得到的完整错误是:

  

类型&#39; System.NotSupportedException&#39;的例外情况发生在EntityFramework.SqlServer.dll中但未在用户代码中处理

     

附加信息:无法创建类型为System.Object&#39;的常量值。在此上下文中仅支持原始类型或枚举类型。

ModelBookings的代码:

namespace BookingsModel
{
    using System;
    using System.Data.Entity;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Linq;

    public partial class ModelBookings : DbContext
    {
        public ModelBookings()
            : base("name=ModelBookingsdb")
        {
        }

        public virtual DbSet<Booking> Bookings { get; set; }
        public virtual DbSet<BookingAttendance> BookingAttendances { get; set; }
        public virtual DbSet<BookingTime> BookingTimes { get; set; }
        public virtual DbSet<Cost> Costs { get; set; }
        public virtual DbSet<Invoice> Invoices { get; set; }
        public virtual DbSet<PaymentMethod> PaymentMethods { get; set; }
        public virtual DbSet<Company> Companies { get; set; }
        public virtual DbSet<CompanyOpeningHour> CompanyOpeningHours { get; set; }
        public virtual DbSet<Industry> Industries { get; set; }
        public virtual DbSet<CompanyReview> CompanyReviews { get; set; }
        public virtual DbSet<Customer> Customers { get; set; }
        public virtual DbSet<Equiptment> Equiptments { get; set; }
        public virtual DbSet<EquiptmentType> EquiptmentTypes { get; set; }
        public virtual DbSet<EquiptmentNeeded> EquiptmentNeededs { get; set; }
        public virtual DbSet<Service> Services { get; set; }
        public virtual DbSet<ServiceType> ServiceTypes { get; set; }
        public virtual DbSet<Staff> Staffs { get; set; }
        public virtual DbSet<StaffAccess> StaffAccesses { get; set; }
        public virtual DbSet<StaffRole> StaffRoles { get; set; }
        public virtual DbSet<SystemAuthorization> SystemAuthorizations { get; set; }
        public virtual DbSet<ProductImage> ProductImages { get; set; }
        public virtual DbSet<ProductSupplier> ProductSuppliers { get; set; }
        public virtual DbSet<Stock> Stocks { get; set; }
        public virtual DbSet<StockLevel> StockLevels { get; set; }
        public virtual DbSet<StockType> StockTypes { get; set; }
        public virtual DbSet<Service_Products> Service_Products { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Booking>()
                .HasMany(e => e.BookingAttendances)
                .WithRequired(e => e.Booking)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<Booking>()
                .HasMany(e => e.BookingTimes)
                .WithRequired(e => e.Booking)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<Booking>()
                .HasMany(e => e.Invoices)
                .WithRequired(e => e.Booking)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<Cost>()
                .HasMany(e => e.Invoices)
                .WithRequired(e => e.Cost)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<PaymentMethod>()
                .HasMany(e => e.Invoices)
                .WithRequired(e => e.PaymentMethod)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<Company>()
                .HasMany(e => e.Bookings)
                .WithRequired(e => e.Company)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<Company>()
                .HasMany(e => e.CompanyOpeningHours)
                .WithRequired(e => e.Company)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<Company>()
                .HasMany(e => e.CompanyReviews)
                .WithRequired(e => e.Company)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<Company>()
                .HasMany(e => e.Staffs)
                .WithRequired(e => e.Company)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<Industry>()
                .HasMany(e => e.Companies)
                .WithRequired(e => e.Industry)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<Customer>()
                .HasMany(e => e.Bookings)
                .WithRequired(e => e.Customer)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<Customer>()
                .HasMany(e => e.BookingAttendances)
                .WithRequired(e => e.Customer)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<Customer>()
                .HasMany(e => e.CompanyReviews)
                .WithRequired(e => e.Customer)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<Equiptment>()
                .HasMany(e => e.EquiptmentNeededs)
                .WithRequired(e => e.Equiptment)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<EquiptmentType>()
                .HasMany(e => e.Equiptments)
                .WithRequired(e => e.EquiptmentType)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<Service>()
                .HasMany(e => e.Bookings)
                .WithRequired(e => e.Service)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<Service>()
                .HasMany(e => e.Costs)
                .WithRequired(e => e.Service)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<Service>()
                .HasMany(e => e.EquiptmentNeededs)
                .WithRequired(e => e.Service)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<Service>()
                .HasMany(e => e.Service_Products)
                .WithRequired(e => e.Service)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<ServiceType>()
                .HasMany(e => e.Services)
                .WithRequired(e => e.ServiceType)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<StaffRole>()
                .HasMany(e => e.Staffs)
                .WithRequired(e => e.StaffRole)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<StaffRole>()
                .HasMany(e => e.StaffAccesses)
                .WithRequired(e => e.StaffRole)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<SystemAuthorization>()
                .HasMany(e => e.StaffAccesses)
                .WithRequired(e => e.SystemAuthorization)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<ProductImage>()
                .HasMany(e => e.Stocks)
                .WithRequired(e => e.ProductImage)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<ProductSupplier>()
                .HasMany(e => e.Stocks)
                .WithRequired(e => e.ProductSupplier)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<Stock>()
                .HasMany(e => e.Service_Products)
                .WithRequired(e => e.Stock)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<Stock>()
                .HasMany(e => e.StockLevels)
                .WithRequired(e => e.Stock)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<StockType>()
                .HasMany(e => e.Stocks)
                .WithRequired(e => e.StockType)
                .HasForeignKey(e => e.ProductStockTypeID)
                .WillCascadeOnDelete(false);
        }
    }
}

错误指向index.cshtml文件中的一行代码:

<th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>

0 个答案:

没有答案