无法在控制器创建时检索元数据



我正在尝试为实体框架创建带有vies的控制器。作为模型类,我将使用产品类:

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

为什么抛出此错误以及我可以用它做什么?

以下是您可以执行的可能修复:

1(如果您使用的是代码优先和错误消息,指示实体"X"没有定义键,或者实体集"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; }
}

之后,确保所有外键和表关系都排列良好(如果需要,请添加ForeignKeyAttributeICollection(。

2(如果错误消息指示无法识别的元素"提供程序">,则 web.config 中的此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 连接字符串(。

引用:

无法使用实体框架创建控制器 - 无法检索 '' 的元数据

实体框架:无法识别的元素"提供程序"异常

好的,我已经解决了。 实际问题出在链接类上。

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

最新更新