2017-08-02 120 views
2

我正在尝试为实体框架创建控制器。作为模型类我将使用产品类别:控制器创建时无法检索元数据

public class Product 
{ 
    public int ProductId { get; set; } 
    public int CategoryId { get; set; } 
    public int ManufacturerId { get; set; } 
    public string Name { get; set; } 
    public string Description { get; set; } 
    public decimal Price { get; set; } 
    public string PhotoUrl { get; set; } 
    public Category Category { get; set; } 
    public Manufacturer Manufacturer { get; set; } 
} 

而像数据上下文类这样的:

public class RetroGadgetEntities:DbContext 
{ 
    public DbSet<Product> Products { get; set; } 
    public DbSet<Category> Categories { get; set; } 
} 

的问题是,我试图创建控制器“无法检索时,得到一个错误'RetroGadget.Models.Product'的元数据“。 据我了解,当代码生成器试图创建强类型的视图时,它实际上是抛出,但我不明白为什么。

UPD: 这是我的Web.config。

<connectionStrings> 
    <add name="RetroGadgetCon" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=RetroGadget.Models.RetroGadgetEntities;Integrated Security=True;" providerName="System.Data.SqlClient"/> 
    </connectionStrings> 
    <entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> 
     <parameters> 
     <parameter value="mssqllocaldb" /> 
     </parameters> 
    </defaultConnectionFactory> 
    <providers> 
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
    </providers> 
    </entityFramework> 

UPD2

public class Product 
{ 
    [Key] 
    public int ProductId { get; set; } 
    public int CategoryId { get; set; } 
    public int ManufacturerId { get; set; } 
    public string Name { get; set; } 
    public string Description { get; set; } 
    public decimal Price { get; set; } 
    public string PhotoUrl { get; set; } 
    [ForeignKey("CategoryId")] 
    public Category Category { get; set; } 
    [ForeignKey("ManufacturerId")] 
    public Manufacturer Manufacturer { get; set; } 
} 

为什么这个错误时,抛出什么我可以做什么呢?

+0

您是否试图通过脚手架模型类使用Code First到控制器?你能否提供web.config内容来分析可能的错误? –

+0

@TetsuyaYamamoto将其添加到UPD部分。 – Gleb

回答

0

好吧,我心中已经解决了这个问题。 实际的问题是链接的类。

public class Category 
{ 
    [Key] 
    public int CategoryId { get; set; } 
    public string Name { get; set; } 
    public string Description { get; set; } 
    public List<Product> Products { get; set; } 
} 

public class Manufacturer 
{ 
    [Key] 
    public int ManufacturerId { get; set; } 
    public string Name { get; set; } 
} 

问题是,有在制造商类没有现场的产品。所以我改变了这个。

public class Manufacturer 
{ 
    [Key] 
    public int ManufacturerId { get; set; } 
    public string Name { get; set; } 
    public List<Product> Products { get; set; } 
} 
1

以下是可能的解决方法,你可以这样做:

1)如果您使用的Code First &错误信息表明实体“X”没有定义EntitySet的“X”键是基于输入“Y”不具有定义键,添加主键属性(KeyAttribute),以用作标识列类属性建模:

using System.ComponentModel.DataAnnotations; 

public class Product 
{ 
    [Key] // add this attribute 
    public int ProductId { get; set; } 

    // other properties 
} 

此外,确保DbContext构造包含引用命名连接字符串在web.config中:

public class RetroGadgetEntities : DbContext 
{ 
    public RetroGadgetEntities() : base("RetroGadgetCon") 
    { 
    } 

    public DbSet<Product> Products { get; set; } 
    public DbSet<Category> Categories { get; set; } 
} 

此后,确保所有外键&表之间的关系被精心布置(添加ForeignKeyAttribute & ICollection如果需要的话)。

2)如果错误消息指示无法识别的元素 '提供商',这provider在幅材部分。配置可能从一个模型创建控制器当EF方面造成问题的元数据(当你降级使用模板前一个默认的EF版本经常出现):

<providers> 
    <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
</providers> 

尝试移除provider一部分,使得entityFramework节成为本:

<entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> 
     <parameters> 
     <parameter value="mssqllocaldb" /> 
     </parameters> 
    </defaultConnectionFactory> 
</entityFramework> 

注:连接字符串部分似乎使用了无效的数据库位置命名空间RetroGadget.Models.RetroGadgetEntities,请尝试更改Initial Catalog使用数据库名称,而不是:

<add name="RetroGadgetCon" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=(database name);Integrated Security=True;" providerName="System.Data.SqlClient"/> 

如果仍不给定LocalDB实例工作的连接,添加AttachDbFilename="(database path)\(database name).mdf" &使用Data Source=(LocalDb)\v11.0(视的LocalDB版本,请参阅SQL Server connection string)。

参考文献:

Cannot create controller with Entity framework - Unable to retrieve metadata for ' '

Entity Framework: Unrecognized element 'providers' exception

+0

谢谢,但没有任何工作。 'RetroGadget.Models.RetroGadgetEntities'是实际的数据库名称(由EF创建)。此外,我还提到,错误消息不会指示任何内容,只是'无法检索'RetroGadget.Models.Product'的元数据,仅此而已。 – Gleb

+0

我不确定错误在“无法提供X的元数据”后面是否包含任何内容。你能否提供更多的细节和/或错误的截图?您的'Product'类也没有定义数据注释,可能错误来自它。 –

+0

你需要什么datails?我可以提供一个截图,但它是俄语。但没有什么更多,只是我提供的错误文本。 – Gleb