无法使用实体框架创建控制器-无法检索元数据 [英] Cannot create controller with Entity framework - Unable to retrieve metadata

查看:47
本文介绍了无法使用实体框架创建控制器-无法检索元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VS2013

当我尝试创建带有使用实体框架的视图的MVC 5控制器" 时,出现以下错误:

When I try to create an "MVC 5 Controller with views using entity Framework" I get the following error:

there was an error running the selected code generator ''Unable to retrieve metadata for WebApplication.Domain.Entities.Product'.'

EFDbContext.cs

using System.Data.Entity;
using WebApplication.Domain.Entities;

namespace WebApplication.Domain.Concrete
{
    public class EFDbContext : DbContext
    {
        public DbSet<Product> Products;
    }
}

Product.cs

using System.ComponentModel.DataAnnotations;

namespace WebApplication.Domain.Entities
{
    public class Product
    {
        [Key]
        public int ProductID { get; set; }

        [Required]
        public string Title { get; set; }
    }
}

Web.config

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <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="EFDbContext" connectionString="Data Source=(LocalDB)\v11.0;Initial Catalog=WebApplication;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>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <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>
</configuration>

我有一个名为Products的表格,其定义如下:

I have a Table set up called Products and it has a definition like so:

CREATE TABLE [dbo].[Products] (
    [ProductID] INT            IDENTITY (1, 1) NOT NULL,
    [Title]     NVARCHAR (255) NULL,
    PRIMARY KEY CLUSTERED ([ProductID] ASC)
);

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

Any ideas what is going wrong? I have tried everything that has returned on Google.

我已安装的软件包:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="EntityFramework" version="6.0.1" targetFramework="net45" />
  <package id="jQuery" version="1.10.2" targetFramework="net45" />
  <package id="jQuery.Validation" version="1.11.1" targetFramework="net45" />
  <package id="Microsoft.AspNet.Mvc" version="5.0.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.Razor" version="3.0.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebPages" version="3.0.0" targetFramework="net45" />
  <package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.0.0" targetFramework="net45" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
  <package id="Unity" version="3.0.1304.1" targetFramework="net45" />
  <package id="Unity.Mvc" version="3.0.1304.0" targetFramework="net45" />
  <package id="WebActivatorEx" version="2.0.4" targetFramework="net45" />
</packages>

我的项目可以在这里找到: https://github.com/jimmyt1988/WebApplication2

My project can be found here: https://github.com/jimmyt1988/WebApplication2

推荐答案

public class EFDbContext : DbContext
{
    public DbSet<Product> Products { get; set; }
}

忘记了{get;放;} ...现在所有作品都可以#crying

Forgot the { get; set; }... all works now #crying

这篇关于无法使用实体框架创建控制器-无法检索元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆