2013-12-09 38 views
16

我使用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> 

我已成立了一個名爲表產品,它有像這樣一個定義:

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

+1

如果你們構建我的項目,試圖添加一個控制器與實體框架視圖時,你會得到相同的結果嗎?仍然無法實現它的工作! – Jimmyt1988

+0

我無法在您的上下文中使用EF Power Tools,我有關於提供者的錯誤消息。也許它不喜歡現有的數據庫。我創建了一個不同名稱的DbContext(EF Power Tools Display EDM Model起作用,這是我通常的酸性測試)。然後我刪除了你的DbContext並重新命名了我的...但後來我嘗試添加一個控制器並得到了類似的結果。我認爲這是一個T4腳手架的錯誤,而不是一個EF錯誤... – ajd

+0

感謝您的幫助哥們,雖然它出來,但看到我的答案.. doh .. noobie錯誤ay – Jimmyt1988

回答

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

忘記了{獲得;組; } ...現在所有作品#crypting

+0

這也可能發生,如果你忘記DbSet的泛型類型參數 from DbSet in your answer)。 – DavidDraughn

4

問題可能是因爲缺少[NotMapped]屬性在模型類之一。

由於我錯過了這個屬性,我狡my了我的頭。

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

這工作對我來說 – Robz

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; } 

     ///.../// 
    } 
0

從您的EFDbContext類中刪除以下行後重試。

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

Example:

+0

這個問題已經回答 – Jimmyt1988

0

在我的情況,但使用VS 2017年,這種情況發生時,我有一個cs文件中定義的所有我的模型類。一旦我將每個類分離到自己的文件中,生成過程就可以正確完成。