tahasozgen
tahasozgen

Reputation: 491

I can not get data from SQL Server via Entity Framework

I try to implement a basic web site that uses EF in ASP.NET MVC. Simply put, I try to connect SQL database and list them. I have a Product model. However, nothing comes to the screen. Here is my Product class:

public class Product
{
    public int ProductID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public decimal Price { get; set; }
    public string Category { get; set; }
}

A repository pattern has been used in the Project. So interface IProductRepository here:

public interface IProductRepository
{
    IQueryable<Product> Products { get; }
}

Here is the Product Controller in the project. It is the only controller in entire solution:

public class ProductController : Controller
{
    private IProductRepository repository;

    public ProductController(IProductRepository productRepository)
    {
        this.repository = productRepository;           
    }

    public ViewResult List()
    {

        //This is not the part of the original project. In order to make sure that the code interacts with
        //the database. 
        if (repository.Products.Count() == 0)
        {

            NinjectControllerFactory nin = new NinjectControllerFactory();
            IKernel kernel = nin.GetNinjectKernel();

            this.repository = kernel.Get<IProductRepository>();
        }

        return View(repository.Products);
    }

}

Default route has been set there. Here is the content of the List.cshtml

@model IEnumerable<Model.Product>

@{
ViewBag.Title = "List";
}

 <h2>List</h2>

 @foreach (var p in Model)
 {
<div class="item">
    <h3>@p.Name</h3>
    @p.Description
    <h4>@p.Price.ToString("c")</h4>
</div>
 }

I connect to my local database, define a table, populate it. I inserted connection string to the web.config of the project:

add name="EFDbContext" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=efver1;Integrated Security=True" providerName="System.Data.SqlClient"

Here is the EFProductRepository.cs

public class EFProductRepository : IProductRepository
{
    private EFDbContext context = new EFDbContext();
    public IQueryable<Product> Products
    {
        get { return context.Products; }
    }
}

and here is the EFDbContext.cs:

public class EFDbContext : DbContext
{
    public DbSet<Product> Products { get; set; }
}

I am using Ninject as a dependency injector. Here it is :

public class NinjectControllerFactory : DefaultControllerFactory
{
    private IKernel ninjectKernel;
    public NinjectControllerFactory()
    {
        ninjectKernel = new StandardKernel();
        AddBindings();
    }
    protected override IController GetControllerInstance(RequestContext
    requestContext, Type controllerType)
    {
        return controllerType == null
        ? null
        : (IController)ninjectKernel.Get(controllerType);
    }
    private void AddBindings()
    {
        ninjectKernel.Bind<IProductRepository>().To<EFProductRepository>();
    }

    public IKernel GetNinjectKernel()        
    {
        return this.ninjectKernel;
    }
}

I made necessary changes here: protected void Application_Start() { AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        //Burası da önemli.
        ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
    }

However, only page I can see is a blank screen as follows :

result

What I am missing here? thanks in advance.

edit 1: Here is the data inside local database: data

Upvotes: 0

Views: 291

Answers (1)

NightOwl888
NightOwl888

Reputation: 56869

add name="EFDbContext" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=efver1;Integrated Security=True" providerName="System.Data.SqlClient"

You are not connecting to the right database. According to the screenshot, your data is in INT-0014\SQLEXPRESS, not (localdb)\v11.0.

Upvotes: 1

Related Questions