【问题标题】:EF Code first query returns 0EF 代码第一次查询返回 0
【发布时间】:2016-03-31 12:33:13
【问题描述】:

我尝试从数据库中的表中获取所有行,但列表返回空。 如果数据库中有行的屏幕截图: https://gyazo.com/37957807543227e4c6b2df9d30b1b970

///Database connectie
var dbconn = new FilmsContext();


///Alles uit de database halen
List<Films> list = dbconn.Films.ToList();

数据库设置

public class FilmsContext : DbContext
    {

        public FilmsContext() : base("FilmsContext")
        {
        }

        public DbSet<Films> Films { get; set; }
    }

表设置

public class Films
    {
        public int Id { get; set; }
        public string name { get; set; }
        public string beschrijving { get; set; }
        public string afbeelding { get; set; }
        public string auteur { get; set; }
        public int sterren { get; set; }
        public string websitelink { get; set; }
        public DateTime datum { get; set; }
    }

这是我的 app.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

【问题讨论】:

  • 视图在哪里..??还有你的连接字符串,因为你提供的代码很好。
  • 显示您的网络配置数据库连接。
  • 检查连接字符串并找出您有多少个数据库副本。
  • 这是我的 app.config

标签: c# database entity-framework code-first


【解决方案1】:

如果您使用的是 SQL CE,则数据库文件可能是在 bin\debug 文件夹中创建的,但您指向的是其他位置。

如果您使用的是DropCreateDatabaseAlways,则尤其如此。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-29
    • 1970-01-01
    • 1970-01-01
    • 2017-06-13
    • 1970-01-01
    • 1970-01-01
    • 2015-10-27
    相关资源
    最近更新 更多