Dbcontext does not give any data

I am using EntityFramework in my ASP.Net MVC project. The problem is that I don't get any data from the database at all. My database is localDB, and the next connection string I use is

<connectionStrings> <add name="PortfolioDBContext" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=NoobMVC;Integrated Security=True" providerName="System.Data.SqlClient"/> </connectionStrings> 

Following is my implementation of the Db context -

 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'm not sure what else I need to do to get the data in DbSet. Am I missing any step here or is there a way to debug the exact problem? I've already searched for SO, and it looks like I'm the only one stuck here.

Update:
I have already tried sending various ways to send data to a view. The main problem is context, not presentation. I do not receive any data in the context, therefore, how I send this data for viewing does not matter.

Update 2: - When you tried to log requests using the ChrFin method, I received 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'm sure something is awkward here. The name of the table is Portfolio, not Products.

Update 3: -
The issue was finally resolved. See below for more details.

0
source share
4 answers

Thanks to the answer, I solved the problem. The database tried to use an old remote table named Products instead of Portfolio. So I deleted the table in SQL Server Object Explorer and renamed the Portfolio table to Products, and DbContext started showing the result.

I thought the variable name DbSet determines the actual name of the table to be used. But I suppose that doesn't work.

PS: Thanks to ChrFin, whose log code solves the problem.

0
source

I think you want something like:

 public ActionResult Index() { using (var data = new PortfolioDBContext()) { var model = data.Portfolio.ToList(); // ToList so the controller queries the DB // does "model" have anything in it here? return View(model); } } 

UPDATE
Try setting the following:

 public class PortfolioDBContext : DbContext { public PortfolioDBContext() { Database.Log = s => Debug.WriteLine(s); } public DbSet<Product> Portfolio { get; set; } } 

and check which requests are sent by EF. Are they right?

UPDATE 2:
You can also do the following if you don't like the table name created by convention:

 [Table("Portfolio")] public class Product { /* ... */ } 

FYI: by convention, the table has a name after the plural of entity names.

+1
source

You pass the whole context to the view. If you are not referring to the Portfolio property inside the view, I think you need to pass the portfolio directly:

return View(data.Portfolio);


From the updated code, I see that you never pass the connection string to DbContext. Use one of the constructors that uses a connection string (the simplest of which is a string whose string is a string)

0
source

Try specifying the name of the connection string directly like this.

 public class PortfolioDBContext : DbContext { public PortfolioDBContext() : base("PortfolioDBContext") { } public DbSet<Product> Portfolio { get; set; } } 
0
source

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


All Articles