我是 Entity Framework 的新手,但我正在尝试根据我在网上找到的一些示例对我的代码进行建模。我以为我已经按照我想要的方式设置了它,但是创建的查询指向错误的、不存在的表。我想知道它从哪里得到表名,因为它是错误的,而且我在任何项目文件中都没有看到指定了这个错误表名的任何地方。这是生成的查询,我从调试中得到:
{SELECT
[Extent1].[AssetTag] AS [AssetTag],
[Extent1].[Location] AS [Location],
[Extent1].[PoolType] AS [PoolType],
[Extent1].[InventoryStatus] AS [InventoryStatus],
[Extent1].[InventoryType] AS [InventoryType],
[Extent1].[Comments] AS [Comments]
FROM [dbo].[ComputerInventories] AS [Extent1]}
ComputerInventories 应该是 ComputerInventory。
这是我的数据模型
public class ComputerInventory
{
[Key]
public String AssetTag {get; set;}
public String Location { get; set; }
public String PoolType { get; set; }
//public String ReasonInProgram { get; set; }
public String InventoryStatus { get; set; }
public String InventoryType { get; set; }
[StringLength(500)]
public String Comments { get; set; }
//public String Clock { get; set; }
}
public class AssetDBContext : DbContext
{
public DbSet<ComputerInventory> ComputerInventory { get; set; }
public AssetDBContext()
: base("Name=<name>")
{
}
}
这是我试图从查询中获取列表的地方
public ViewResult Index()
{
return View(db.ComputerInventory.ToList());
}
我完全不知道从哪里获取表名的 ComputerInventories 值。
最佳答案
Entity Framework 对许多事物使用命名约定。其中之一是表名是通过采用实体名称的复数形式创建的。您可以像这样删除该约定:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
参见 PluralizalingTableNameConvention on MSDN .
有关约定的更多信息
http://entityframework.codeplex.com/wikipage?title=Custom%20Conventions
关于c# - 不确定 DbContext 如何对错误的表创建 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13706874/