我正在尝试为实体框架创建控制器。作为模型类我将使用产品类别:控制器创建时无法检索元数据
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; }
}
为什么这个错误时,抛出什么我可以做什么呢?
您是否试图通过脚手架模型类使用Code First到控制器?你能否提供web.config内容来分析可能的错误? –
@TetsuyaYamamoto将其添加到UPD部分。 – Gleb