实体框架DbContext返回空值

时间:2014-10-01 20:56:47

标签: c# entity-framework visual-studio localdb

我创建了一个DataContext。

namespace Attendance.DataAccess.DAL
{
    using System.Data.Entity;
    using System.Data.Entity.ModelConfiguration.Conventions;
    using Attendance.DataAccess.Models;

    public class AttendanceContext : DbContext
    {
        public DbSet<Person> Persons { get; set; }
        public AttendanceContext()
            : base("AttendanceContext")
        { 

        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }
}

我已经创建了一个配置。

namespace Attendance.DataAccess.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
    using Attendance.DataAccess.Models;
    using Attendance.DataAccess.DAL;

    internal sealed class Configuration : DbMigrationsConfiguration<AttendanceContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(AttendanceContext context)
        {
            context.Persons.AddOrUpdate(
              p => p.FirstName,
              new Person { FirstName = "Andrew Peters" },
              new Person { FirstName = "Brice Lambson" },
              new Person { FirstName = "Rowan Miller" }
            );

        }
    }
}

我已经创建了一个初始迁移。

namespace Attendance.DataAccess.Migrations
{
    using System;
    using System.Data.Entity.Migrations;

    public partial class InitialCreate : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.Person",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        FirstName = c.String(),
                        LastName = c.String(),
                    })
                .PrimaryKey(t => t.Id);

        }

        public override void Down()
        {
            DropTable("dbo.Person");
        }
    }
}

我创建了一个简单的api控制器。

namespace Attendance.WebApi.Controllers
{

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Web.Http;
    using Attendance.DataAccess.Models;
    using Attendance.DataAccess.DAL;
    using System.Data.Entity;

    public class PersonController : ApiController
    {
        // http://localhost/Attendance.WebApi/api/person
        public IEnumerable<Person> GetAllPersons()
        {
            AttendanceContext db = new AttendanceContext();

            var persons = 
                    from person
                    in db.Persons
                    select person;

            // this is always an empty array
            // I.e. the web browser displays <ArrayOfPerson/>
            return persons.ToList();
        }
    }
}

我已将其配置如下:

<connectionStrings>
  <add name="AttendanceContext" 
       connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=AttendanceLocal;Integrated Security=SSPI;" 
       providerName="System.Data.SqlClient"/>
</connectionStrings>

我还使用Server Explorer和Object Explorer直接查询了数据库。数据库在Person表中有行。为什么不在Entity Framework DataContext Person中显示那些行?

1 个答案:

答案 0 :(得分:0)

我需要在Nuget包管理器控制台中运行Update-Database。我查询的数据库没有种子值。