none
CodeFirst EF and connection string by Adam's Freeman book RRS feed

  • Question

  • I'm learning the asp.net mvc 3 of the book of Adam Freeman. In the example, a SportsStore applications using CodeFirst approach. When solution run throws exception: Invalid object name 'dbo.Products'.I'm assuming that the problem in the connection string. My Web.config expample:

    <add name="EFDbContext" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=SportsStore;Integrated Security=True" providerName="System.Data.SqlClient" />
    and csharp code:
    public class EFProductRepository : IProductRepository
    {
        private EFDbContext context = new EFDbContext();
    
        public IQueryable<Product> Products
        {
            get
            {
                return context.Products;
            }
        }
    }
    
    public class EFDbContext : DbContext
    {
        public DbSet<Product> Products { get; set; }
    }

    add code to Global.asax file Application_Start() method:

    ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());

    my Controller:

     public class ProductController : Controller
    {
        private IProductRepository repository;
    
        public ProductController(IProductRepository repoParam)
        {
            repository = repoParam;
        }
    
        public ViewResult List()
        {
            return View(repository.Products);
        }
    }

    exception throws when I'm cast products ToList()

    I downloaded an archive with code examples for this book. Changed web.config. When launched, this solution is an exception generated too Anybody know what the problem? Thanks!


    • Edited by drup1 Friday, July 13, 2012 11:21 AM
    Friday, July 13, 2012 11:18 AM

Answers