简体   繁体   中英

When using generic in the dbset of the entity framework at Migration

I made this change, before it was like this

    public DbSet<Pessoa> Pessoas { get; set; }

Now it's this way

class ContextData<T> : DbContext where T : class
{
    public DbSet<T> Dbset { get; set; }

    public ContextData() : base("name=Connection")
        {

        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {            

        base.OnModelCreating(modelBuilder);

        modelBuilder.Configurations.Add(new PessoaMapping());                          

        }
}        

So now I do not know what I do in class migrations

internal sealed class Configuration : DbMigrationsConfiguration<local.blogapi.Context.ContextData
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
    }

    protected override void Seed(local.blogapi.Context.ContextData context)
    {

    }
}

I know it's going to make a mistake. I do not know how to get the entities or if there is any specific command at the time of doing the migration, or if I pass some interface, such as IEntity

you're probably wrong.

A context is a group of dbset (dbsets represent sql tables), read this .

I think that you don't need one context for every table in your db. Use of more contexts may make sense if you want to separate dbsets that have different logic (based on the schema for example, or to separate read / write tables)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM