2013-12-09 21 views
16

sto usando VS2013Impossibile creare controller con Entity Framework - Impossibile recuperare i metadati

Quando provo a creare un "MVC 5 Regolatore con viste utilizzando Entity Framework" ottengo il seguente errore:

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> 

Ho un tavolo istituito chiamato prodotti ed ha una definizione in questo modo:

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

Qualsiasi idee cosa sta andando male? Ho provato tutto ciò che è tornato su Google.

pacchetti ho installato:

<?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> 

Il mio progetto può essere trovato qui: https://github.com/jimmyt1988/WebApplication2

+1

Se costruisci il mio progetto, ottieni lo stesso risultato quando provi ad aggiungere un controller con le viste del framework di entità? ancora non riesco a farlo funzionare! – Jimmyt1988

+0

Non ho potuto utilizzare gli EF Power Tools sul tuo contesto, ho ricevuto un messaggio di errore sul provider. Forse non gli piaceva il database esistente. Ho creato un DbContext con un nome diverso che ha funzionato correttamente (ha funzionato il modello EDM Display EF Power Tools, che è il mio solito test dell'acido). Ho quindi eliminato il tuo DbContext e rinominato il mio ... ma poi ho provato ad aggiungere un controller e ho ottenuto un risultato simile. Penso che si tratti di un errore di impalcatura T4 non di un errore EF ... – ajd

+0

Grazie per l'amico di aiuto, tuttavia, è possibile vedere la mia risposta .. doh .. noobie error ay – Jimmyt1988

risposta

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

dimenticato il {get; impostato; } ... tutto funziona ora #crying

+0

Questo può accadere anche se si dimentica il parametro di tipo generico di DbSet ( da DbSet nella risposta). – DavidDraughn

4

Il problema potrebbe essere dovuto alla mancanza di [NotMapped] Attributo in una classe del modello.

Come ho perso l'attributo e sono stato furbo la testa.

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

Questo ha funzionato per me – Robz

1

Si prega di verificare tutti i modelli sono registrati nel contesto

Esempio:

Hai un modello come questo

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

Controllare tutti virtual le istanze del modello sono registrati in in contesto come questo

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

Riprovare dopo aver eliminato la riga sotto dalla classe EFDbContext.

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

Example:

+0

Questa domanda ha già una risposta – Jimmyt1988

0

Nel mio caso, ma utilizzando VS 2017, questo si è verificato quando ho avuto tutti i miei classi del modello definite all'interno di un singolo file con estensione cs. Una volta separato ogni classe nel proprio file, il processo di generazione è stato completato correttamente.

Problemi correlati