无效的对象名称&#d ;.Tasks'。在Code First方法中

时间:2017-04-21 11:47:31

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

我使用代码优先方法连接数据库和表但由于某些问题启用/添加迁移命令不创建我的表,所以我手动创建表。应用程序构建成功,这意味着我假设objDbContext获取我的表。 Table的名称是数据库中的Task。 以下是我的代码

eDbContext objDbContext = new eDbContext ();
 public List<TaskDetail> GetTasks(long eventId)
        {
            List<TaskDetail> listTask = new List<TaskDetail>();
            try {

                listTask = (from task in objDbContext.Tasks
                            where task.EventId==eventId
                            select new TaskDetail
                            {
                                Id = task.Id,
                                Title = task.Title,
                                Description = task.Description,
                                StartDate = task.StartDate,
                                EndDate = task.EndDate
                            }
                            ).ToList();
            }

            catch(Exception ex) {
                throw ex;
            }
            return listTask;

        }

以下是数据库上下文

 public class eDbContext : DbContext
    {
        public DbSet<Task> Tasks { get; set; }
    }

2 个答案:

答案 0 :(得分:2)

如果您有其他实体的类似问题(复数表名),则应删除PluralizingTableNameConvention(默认情况下,EF会从实体类型名称生成多个表名)。将此代码添加到DbContext类:

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

如果其他表有多个名称,那么您应该修复Task实体的映射,如@Valkyriee建议的那样。

答案 1 :(得分:0)

您的DbContext类应如下所示:

public class eDbContext : DbContext
{
    public IebContext()
        : base("name=ConnectionStringName")
    {

        Database.SetInitializer(new MigrateDatabaseToLatestVersion<eDbContext, Migrations.Configuration>("CatalogName"));

    }
    public DbSet<Task> Tasks{ get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new TaskMap());
    }

}

对于您的迁移,您可以创建一个新类,如:

internal sealed class Configuration : DbMigrationsConfiguration<eDbContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
        //know this might loss data while its true. 
        AutomaticMigrationDataLossAllowed = true;
        ContextKey = "Path to your DbContext Class";
    }

    protected override void Seed(eDbContext context)
    {
        //  This method will be called after migrating to the latest version.

        //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
        //  to avoid creating duplicate seed data. E.g.
        //
        //    context.People.AddOrUpdate(
        //      p => p.FullName,
        //      new Person { FullName = "Andrew Peters" },
        //      new Person { FullName = "Brice Lambson" },
        //      new Person { FullName = "Rowan Miller" }
        //    );
        //
    }
}

现在使用这种方法,您可以先使用EF代码创建表格,然后再进行更改。请注意,我已为任务添加了Map类,这意味着我使用流畅的api来映射我的实体:

public class TaskMap : EntityTypeConfiguration<Task>
{
    public TaskMap ()
    {
        ToTable("Tasks");
        HasKey(x => x.Id);
    }
}