问题描述
我用我的ASP.NET MVC项目的EntityFramework。问题是我不是从数据库中获取任何数据。
我的数据库是一个的LocalDB和下面是我使用的连接字符串 -
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);
}
}
我不知道还有什么我需要才能在DbSet得到的数据做。我在这里缺少任何步骤,或有什么办法来调试确切的问题?我已经搜查SO,看起来像我是谁的困在这里唯一的一个。
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.
更新2: -
当试图登录使用ChrFin的方法查询,我得到了以下日志 -
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.
更新3: - 结果
这个问题得到了最终解决了。请在下面检查我的回答的详细信息。
Update 3:-
The issue got solved finally. Please check my answer below for details.
推荐答案
感谢所有的答案,我得到了这个问题解决了。该数据库试图使用名为产品,而不是投资组合的老删除的表。所以我删除了SQL Server的对象资源管理器表,并更名为'组合'表'产品'和的DbContext开始显示的结果。
Thanks to all of the answer, I got the issue solved. The database was trying to use an old deleted table named 'Products' instead of 'Portfolio'. So I deleted the table in the SQL Server Object Explorer and renamed the 'Portfolio' table to 'Products' and the DbContext started showing the result.
我认为DbSet变量的名称决定要使用的实际的表的名称。但我想它不会这样的。
I thought the name of the DbSet variable decides the actual table name to be used. But I suppose it doesn't work that way.
PS:感谢ChrFin的日志code导致解决问题
PS: Thanks to ChrFin whose log code lead to solving the issue.
这篇关于的DbContext不给任何数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!