Not sure how DbContext creates an SQL query on an invalid table

I am new to the Entity Framework, but I am trying to model my code after some examples that I found on the Internet. I thought I configured it the way I wanted, but the query created indicates an invalid, non-existent table. I am wondering where the table name comes from, because it is incorrect, and I do not see anywhere in any project file that this incorrect table name was indicated. Here is the request that was generated and I received from debugging:

{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 must be ComputerInventory.

Here is my data model

 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>") { } } 

Here I am trying to get a list from a query

 public ViewResult Index() { return View(db.ComputerInventory.ToList()); } 

I completely lost information about where it gets the ComputerInventories value for the table name.

+4
source share
2 answers

Entity Framework uses naming conventions for many things. One of them is that table names are created by using the plural of the entity name. You can delete this agreement as follows:

 protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); } 

See PluralizalingTableNameConvention on MSDN .

More on agreements

http://entityframework.codeplex.com/wikipage?title=Custom%20Conventions

+4
source

Although this has already been answered, I would like to add an alternative for any future visitors to this question ...

Instead of deleting the agreement, simply provide the table name by overriding the same method.

 protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Object>().ToTable("TableName"); } 
+3
source

All Articles