Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF CTP5 error: Invalid object name

I followed the example on scottgu's blog about EF code first CTP5 but I get the 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 done the same as the blogpost, its not my first time with EF (But CTP5 tho), I'm I overlooking something?

like image 677
Dejan.S Avatar asked Dec 01 '22 03:12

Dejan.S


1 Answers

If your table name is Product in the database, try this:

[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 will need to add the following using statement:

using System.ComponentModel.DataAnnotations;

Hope this helps! It worked for me.

like image 127
Omar Abdelhaq Avatar answered Dec 10 '22 23:12

Omar Abdelhaq