DbContext没有提供任何数据

时间:2014-10-16 12:43:47

标签: c# asp.net asp.net-mvc entity-framework dbcontext

我在ASP .Net MVC项目中使用EntityFramework。问题是我根本没有从数据库中获取任何数据。 我的数据库是localDB,以下是我正在使用的连接字符串 -

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

以下是我的Db上下文实现 -

public class PortfolioDBContext : DbContext
    {
        public PortfolioDBContext()
        {
            Debug.Write("Noob CONNNNN  "+Database.Connection.ConnectionString);
        }
        public DbSet<Product> Portfolio { get; set; }
    }

这就是我的控制器的样子 -

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,看起来我是唯一一个被困在这里的人。

更新
我已经尝试过发送各种方式向视图发送数据。主要问题在于背景,而不是视图。我没有在上下文中获取任何数据,因此我将这些数据发送给查看的方式并不重要。

更新2: - 当尝试使用ChrFin方法记录查询时,我得到了以下日志 -

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

我确信这里有些可疑。表的名称是Portfolio,而不是Products。

更新3: -
问题最终得到了解决。请查看下面的答案以获取详细信息。

4 个答案:

答案 0 :(得分:1)

我认为你想要的是:

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);
    }
}

<强>更新
尝试设置以下内容:

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

并检查EF发送的查询。它们是否正确?

更新2:
如果您不喜欢通过约定创建的表名,也可以执行以下操作:

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

仅供参考:根据惯例,该表以多个实体名称命名。

答案 1 :(得分:0)

您正在将整个上下文传递给View。除非您在视图中引用Portfolio属性,否则我认为您需要直接传递投资组合:

return View(data.Portfolio);


从更新的代码中,我发现你永远不会将连接字符串传递给DbContext。使用其中一个使用连接字符串的constructors(最简单的是一个字符串,字符串作为连接字符串)

答案 2 :(得分:0)

尝试直接指定连接字符串的名称。

public class PortfolioDBContext : DbContext
{
    public PortfolioDBContext() : base("PortfolioDBContext") { }

    public DbSet<Product> Portfolio { get; set; }
}

答案 3 :(得分:0)

感谢所有答案,我解决了问题。该数据库试图使用名为&#39; Products&#39;而不是&#39;投资组合&#39;。所以我删除了SQL Server对象资源管理器中的表,并重命名了&#39; Portfolio&#39;表到&#39;产品&#39;并且DbContext开始显示结果。

我认为DbSet变量的名称决定了要使用的实际表名。但我认为它不会那样工作。

PS:感谢ChrFin的日志代码导致解决问题。