I am using EntityFramework in my ASP .Net MVC project. The problem is I am not getting any data from the Database at all. My Database is a localDB and following is the connection string I am using -
<connectionStrings>
<add name="PortfolioDBContext" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=NoobMVC;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
Following is my Db Context implementation -
public class PortfolioDBContext : DbContext
{
public PortfolioDBContext()
{
Debug.Write("Noob CONNNNN "+Database.Connection.ConnectionString);
}
public DbSet<Product> Portfolio { get; set; }
}
And this is what my controller looks like -
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
PortfolioDBContext data = new PortfolioDBContext();
Debug.Write("Data size " + data.Portfolio.Count()); //This prints 0
return View(data);
}
}
I am not sure what else I need to do in order to get data in the DbSet. Am I missing any step here or is there any way to debug the exact issue? I've already searched on SO and looks like I am the only one who's stuck here.
Update:
I've already tried sending various way to send data to the view. The main problem lies with the context, not with the view. I am not getting any data in the context, thus how I send this data to view doesn't matter.
Update 2:- When tried to log the queries using ChrFin's method, I got the following logs -
Opened connection at 16-10-2014 06:32:07 PM +05:30
SELECT Count(*)
FROM INFORMATION_SCHEMA.TABLES AS t
WHERE t.TABLE_SCHEMA + '.' + t.TABLE_NAME IN ('dbo.Products')
OR t.TABLE_NAME = 'EdmMetadata'
-- Executing at 16-10-2014 06:32:08 PM +05:30
-- Completed in 21 ms with result: 1
Closed connection at 16-10-2014 06:32:08 PM +05:30
Opened connection at 16-10-2014 06:32:08 PM +05:30
SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
COUNT(1) AS [A1]
FROM [dbo].[__MigrationHistory] AS [Extent1]
WHERE [Extent1].[ContextKey] = @p__linq__0
) AS [GroupBy1]
-- p__linq__0: 'Noob_MVC.Models.PortfolioDBContext' (Type = String, Size = 4000)
-- Executing at 16-10-2014 06:32:08 PM +05:30
-- Completed in 21 ms with result: SqlDataReader
Closed connection at 16-10-2014 06:32:08 PM +05:30
Opened connection at 16-10-2014 06:32:08 PM +05:30
SELECT TOP (1)
[Project1].[C1] AS [C1],
[Project1].[MigrationId] AS [MigrationId],
[Project1].[Model] AS [Model],
[Project1].[ProductVersion] AS [ProductVersion]
FROM ( SELECT
[Extent1].[MigrationId] AS [MigrationId],
[Extent1].[Model] AS [Model],
[Extent1].[ProductVersion] AS [ProductVersion],
1 AS [C1]
FROM [dbo].[__MigrationHistory] AS [Extent1]
WHERE [Extent1].[ContextKey] = @p__linq__0
) AS [Project1]
ORDER BY [Project1].[MigrationId] DESC
-- p__linq__0: 'Noob_MVC.Models.PortfolioDBContext' (Type = String, Size = 4000)
-- Executing at 16-10-2014 06:32:08 PM +05:30
-- Completed in 17 ms with result: SqlDataReader
Closed connection at 16-10-2014 06:32:08 PM +05:30
Opened connection at 16-10-2014 06:32:08 PM +05:30
SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
COUNT(1) AS [A1]
FROM [dbo].[Products] AS [Extent1]
) AS [GroupBy1]
-- Executing at 16-10-2014 06:32:08 PM +05:30
-- Completed in 10 ms with result: SqlDataReader
Closed connection at 16-10-2014 06:32:08 PM +05:30
Noob context size 0Opened connection at 16-10-2014 06:32:08 PM +05:30
'iisexpress.exe' (CLR v4.0.30319: /LM/W3SVC/21/ROOT-1-130579381242638924): Loaded 'EntityFrameworkDynamicProxies-Noob MVC'.
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Name] AS [Name],
[Extent1].[Description] AS [Description],
[Extent1].[Link] AS [Link],
[Extent1].[SmallImageLink] AS [SmallImageLink],
[Extent1].[LargeImageLink] AS [LargeImageLink]
FROM [dbo].[Products] AS [Extent1]
-- Executing at 16-10-2014 06:32:08 PM +05:30
-- Completed in 11 ms with result: SqlDataReader
Closed connection at 16-10-2014 06:32:08 PM +05:30
I am sure something is fishy here. The name of the table is Portfolio, not Products.
Update 3:-
The issue got solved finally. Please check my answer below for details.