表格未使用EF代码优先方法进行映射 [英] Table not mapped using EF code first approach

查看:91
本文介绍了表格未使用EF代码优先方法进行映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用EF code first approach.我添加了数据库,并生成了表.然后我添加了这个课程

I'd like to use EF code first approach. I added the database and I generate the tables . Then I added this class

  public class Invitation
    {

      
        [Key]
        public int Id { get; set; }

        [DefaultValue(false)]
        public bool State { get; set; }

        public string Mail { get; set; }

        public string Tel { get; set; }

        public string Name { get; set; }

        public string Qr_code { get; set; }
  
    }

然后我运行这些命令:

第二次添加迁移

add-migrations second

更新数据库

第二类迁移的

UpDown方法为空!并且没有将表添加到数据库中.

the Up and Down methods of the second class migration are empty!! and no table is added to the database.

上下文

 public class ApplicationContext: IdentityDbContext<ApplicationUser>
    {
         public ApplicationContext()
            :base("DefaultConnection")
        {
            Database.SetInitializer<ApplicationContext>(new CreateDatabaseIfNotExists<ApplicationContext>());
        }

        public static ApplicationContext Create()
        {
            return new ApplicationContext();
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
        }
    }

所以我需要知道

  1. 此问题的原因是什么?
  2. 我该如何解决?

推荐答案

好像您忘了告诉Entity Framework您要添加的新表(DbSet<Invitation>)

Looks like you forgot to tell Entity Framework about the new table that you want added (DbSet<Invitation>)

添加此内容后,实体框架应分别添加要在迁移"脚本中添加的表.

Once you add this, Entity Framework should add the table(s) you want added in the Migration script, respectively.

总而言之,您需要添加以下行:

In summation, you would need to add this line :

public DbSet<Invitation> Invitations { get; set; }

和/或

public IDbSet<Invitation> Invitations { get; set; }

并运行另一个迁移脚本.

and run another Migration Script.

这篇关于表格未使用EF代码优先方法进行映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆