无法使用Entity框架创建控制器 - 无法检索元数据

问题描述 投票:17回答:6

我正在使用VS2013

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

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的表设置,它的定义如下:

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

出了什么问题?我已经尝试过在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

c# entity-framework visual-studio-2013
6个回答
8
投票
public class EFDbContext : DbContext
{
    public DbSet<Product> Products { get; set; }
}

忘了{get;组; } ...现在一切正常#crying


4
投票

问题可能是因为在其中一个模型类中缺少[NotMapped]属性。

因为我错过了这个属性而我正在狡猾地说。

[Display(Name="Logo")]
[DataType(DataType.Upload)]
[NotMapped]
public HttpPostedFileBase Logo { set; get; }

1
投票

请检查所有模型是否已在上下文中注册

例:

你有这样的模特

public class MedicineStrength
{
    [Key]
    public int Id { get; set; }
    public int? MedicineId { get; set; }
    [ForeignKey("MedicineId")]
    public Medicine Medicine { get; set; }

    public double Strength { get; set; }

    public int? MetricPrefixId { get; set; }
    [ForeignKey("MetricPrefixId")]
    public virtual MetricPrefix MetricPrefix { get; set; }

    public int? UnitId { get; set; }
    [ForeignKey("UnitId")]
    public virtual Unit Unit { get; set; }

    public int? MedicineTypeId { get; set; }
    [ForeignKey("MedicineTypeId")]
    public virtual MedicineType MedicineType { get; set; }
}

检查所有virtual模型实例是否在此上下文中注册

public class HMContext : DbContext
    {
        public HMContext()
        : base("name=HMContext")
        {
            Database.SetInitializer<HMContext>(null);
        }

        /// ... ///

        public virtual DbSet<Medicine> Medicines { get; set; }
        public virtual DbSet<MedicineGeneric> MedicineGenerics { get; set; }
        public virtual DbSet<MedicineStrength> MedicineStrengths { get; set; }
        public virtual DbSet<MedicineType> MedicineTypes { get; set; }
        public virtual DbSet<MedicineDosage> MedicineDosages { get; set; }
        public virtual DbSet<MedicineCategory> MedicineCategories { get; set; }
        public virtual DbSet<MetricPrefix> MetricPrefixes { get; set; }
        public virtual DbSet<Unit> Units { get; set; }

        ///...///
    } 

1
投票

从EFDbContext类中删除以下行后重新尝试。

public System.Data.Entity.DbSet<WebApplication.Domain.Entities.Product> Products { get; set; }

Example:


0
投票

在我的情况下,但是使用VS 2017,当我在单个.cs文件中定义了所有模型类时,就会发生这种情况。一旦我将每个类分成了自己的文件,生成过程就会正确完成。


0
投票

如果您尝试创建Odata控制器

转到DbContext类,然后检查是否

public System.Data.Entity.DbSet<BackEnd.Models.OrganizationType> YourModel{ get; set; }

可用与否

如果是,请将其删除然后重试

© www.soinside.com 2019 - 2024. All rights reserved.