无法使用Entity Framework创建控制器-这是运行所选代码生成器时发生的错误“无法检索

时间:2020-04-13 16:37:23

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

我正在使用VS2017。当我尝试创建“带有使用Entity Framework的视图的ASP.NET MVC 5控制器”时,出现以下错误:

Error

这是我的模型类,我使用DbSet<Booking>进行设置,但仍然收到此错误。

IdentityModels.cs

using System.Data.Entity;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;

namespace EventManagment.Models
{
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public DbSet<UserData> UserDatas { get; set; }
        public DbSet<Booking> Bookings { get; set; }
        public DbSet<Flower> Flowers { get; set; }
        public DbSet<Equipment> Equipment { get; set; }
        public DbSet<Food> Foods { get; set; }
        public DbSet<Seating> Seatings { get; set; }
        public DbSet<Lighting> Lightings { get; set; }
        public DbSet<Venue> Venues { get; set; }
        public DbSet<EventType> EventTypes { get; set; }

        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }
}

Booking.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
using EventManagment.Models;
using Microsoft.AspNet.Identity;

namespace EventManagment.Models
{
    public class Booking
    {
        [Key]
        public int BookingID { get; set; }

        //[Display(Name ="CNIC")]
        //public String UCNIC { get; set; }
        public String Id { get; set; }

        [Display(Name ="Event Type")]
        public int EventID { get; set; }

        [Display(Name ="No Of Guest")]
        public int NoOfGuest { get; set; }

        [DataType(DataType.Date)]
        [Display(Name ="Booking Date")]
        public DateTime BookingDate { get; set; }

        [Display(Name ="Equipment Type")]
        public int EquipmentID{ get; set; }

        [Display(Name = "Flower Plan")]
        public int FlowerID { get; set; }

        [Display(Name = "Food Type")]
        public int FoodID { get; set; }

        [Display(Name = "Lighting Plan")]
        public int LightingID{ get; set; }

        [Display(Name = "Seating Plan")]
        public int SeatingID { get; set; }

        [Display(Name = "Venue Name")]
        public int VenueID { get; set; }

        //public UserData UserData { get; set; }
        [ForeignKey("Id")]
        public ApplicationUser User { get; set; }
        public EventType EventType { get; set; }
        public Equipment Equipment { get; set; }
        public Flower Flower { get; set; }
        public Food Food { get; set; }
        public Lighting Lighting { get; set; }
        public Seating Seating { get; set; }
        public Venue Venue { get; set; }
    }
}

web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="entityFramework" 
                 type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
                 requirePermission="false" />
    </configSections>
    <connectionStrings>
        <add name="DefaultConnection" 
             connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-EventManagment-20200322091502.mdf;Initial Catalog=aspnet-EventManagment-20200322091502;Integrated Security=True" 
             providerName="System.Data.SqlClient" />
    </connectionStrings>
    <appSettings>
        <add key="webpages:Version" value="3.0.0.0" />
        <add key="webpages:Enabled" value="false" />
        <add key="ClientValidationEnabled" value="true" />
        <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    </appSettings>
    <system.web>
        <authentication mode="None" />
        <compilation debug="true" targetFramework="4.6.1" />
        <httpRuntime targetFramework="4.6.1" />
    </system.web>
    <system.webServer>
        <modules>
            <remove name="FormsAuthentication" />
        </modules>
    </system.webServer>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" />
                <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" />
                <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" />
                <bindingRedirect oldVersion="0.0.0.0-4.0.0.0"   newVersion="4.0.0.0" />
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" />
                <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" />
                <bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
                <bindingRedirect oldVersion="0.0.0.0-1.1.0.0"   newVersion="1.1.0.0" />
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
                <bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" />
                <bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
                <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
                <bindingRedirect oldVersion="0.0.0.0-5.2.4.0" newVersion="5.2.4.0" />
            </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Syncfusion.Licensing" publicKeyToken="632609b4d040f6b4" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-17.4460.0.55" newVersion="17.4460.0.55" />
      </dependentAssembly>
    </assemblyBinding>
    </runtime>
    <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
        <providers>
            <provider invariantName="System.Data.SqlClient" 
                      type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        </providers>
    </entityFramework>
    <system.codedom>
        <compilers>
            <compiler language="c#;cs;csharp" extension=".cs" 
                      type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
                      warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
            <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" 
                      type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
                      warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
        </compilers>
    </system.codedom>
</configuration>

任何想法出了什么问题?我已经尝试了所有在Google上返回的内容。

1 个答案:

答案 0 :(得分:2)

将web.config文件中的更改作为 数据源=。\ MSSQLLocalDB;