EF CTP5 Error: Invalid Object Name

I followed the example on the scottgu blog about EF code first CTP5, but I get an error that

System.Data.SqlClient.SqlException: Invalid object name 'dbo.Products'.

this is the code i got.

<add name="CTP5Context" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|EFCTP5.mdf;User Instance=true" providerName="System.Data.SqlClient" /> public class CTP5Context : DbContext { public DbSet<Product> Products { get; set; } } public class Product { public int Id { get; set; } public string ProductName { get; set; } public int Amount { get; set; } } var context = new CTP5Context(); var products = context.Products; return View(products); 

im kinda clueless here I did the same as the blogpost, this is not my first time with EF (but CTP5 tho), am I missing something?

+4
source share
5 answers

If your table name is a product in the database, try the following:

 [Table("Product", SchemaName = "dbo")] public class Product { public int Id { get; set; } public string ProductName { get; set; } public int Amount { get; set; } } 

To use the Table attribute, you need to add the following statement:

 using System.ComponentModel.DataAnnotations; 

Hope this helps! It worked for me.

+7
source

I had the same problem, but I made 2 changes and it works for me. I changed the connection string (initial directory added)

 <add name="CTP5Context" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\northwind.mdf;User Instance=true;initial catalog=Northwind" providerName="System.Data.SqlClient" /> 

and in Global.asax I added the following line to Application_Start ()

 Database.SetInitializer<Northwind>(new System.Data.Entity.DropCreateDatabaseAlways<Northwind>()); 
+5
source

The exception looks like it comes from a database. Are you sure your table name is “Products” or is it “Product” (singular instead of plural?)

+3
source

EF Code First seems to work differently depending on the type of database you are connecting to. If you are working with SQLCE, which ScottGu uses to display EF Code First, then all tables will be created with names that are not plural. However, if you are using SQL Server 2008 (what I tested), it was expected that the table names would be multiple. There are several ways around this, you can add an attribute to the table name, as Omar shows, or you can override the OnModelCreating event for the context.

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

Make sure that the product table was created using the dbo scheme, many times the scheme will be something other than dbo , for example, it can be your username or server name (if the account you are working with is not in the db_owner scheme). Open DB (starting with SQLExpress) using Server Explorer in Visual Studio)

To do this, right-click on the table name and select Open table definition , then in the table definition, right-click and select Properties , and in the properties window, check what is indicated in the list in the schema value. If this is not a DBO, then you should be able to change it to dbo and save it.

+1
source

Source: https://habr.com/ru/post/1336026/


All Articles