Popular table with fluent api

1

I have this Many to Many template and I need to popular the table through the Seed file as described at the end of the post using api fluent / p>

Contextfile:

publicEfDbContext():base("EfDbContext") { }
        public DbSet<TBUsuario> Usuarios { get; set; }
        public DbSet<TBMenu> Menus { get; set; }
        public DbSet<TBPerfil> Perfis { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            modelBuilder.Entity<TBUsuario>().ToTable("TBUsuario");
            modelBuilder.Entity<TBPerfil>().ToTable("TBPerfil");

            modelBuilder.Entity<TBMenu>()
                .HasMany<TBPerfil>(p => p.Perfis)
                .WithMany(m => m.Menus)
                .Map(pm => 
                        {
                            pm.MapLeftKey("TBMenuID");
                            pm.MapRightKey("TBPerfilID");
                            pm.ToTable("TBMenuPerfil");
                        });
        }

Seed:

protected override void Seed(Lamar.Dominio.Repositorio.EfDbContext context)
        {
            var perfil = new List<TBPerfil>
                {
                    new TBPerfil {PerfilID=1, Nome="Administrador" },
                    new TBPerfil {PerfilID=2, Nome="Operacional" }
                };
            perfil.ForEach(s => context.Perfis.AddOrUpdate(p => p.PerfilID, s));
            context.SaveChanges();

            var menus = new List<TBMenu>
                {
                    new TBMenu{TBMenuID=1, Action="#", Controller=string.Empty, Icon="fui-user", Nome="Cadastros", Posicao=0, Tooltip="Cadastros" },
                    new TBMenu{TBMenuID=2, Action="Index", Controller="Clientes", Icon=string.Empty, Nome="Clientes", Posicao=1, Tooltip="Clientes" }
                };
            menus.ForEach(s => context.Menus.AddOrUpdate(p => p.TBMenuID, s));
            context.SaveChanges();            
        }
    
asked by anonymous 22.06.2016 / 00:22

1 answer

1

The TBMenuProfile table is an auxiliary table. The Entity Framework handles it internally. You will only manipulate your models, as always. But you need to get the object after it has been added to DbSet

Method for adding and retrieving objects:

public static T AddNew<T>(this EfDbContext context, T entity)
    where T : class
{
    var dbSet = context.Set<T>();

    dbSet.Add(entity);

    return entity;
}

Seed:

private TBPerfil Perfil1 { get;set; }
private TBPerfil Perfil2 { get;set; }    

this.Perfil1 = this.Context.AddNew(new TBPerfil 
{
    PerfilID=1, Nome="Administrador"
};
this.Perfil2 = this.Context.AddNew(new TBPerfil 
{
    PerfilID=2, Nome="Operacional"
};

private TBMenu Menu1 {get;set;}

    this.Menu1 = this.Context.AddNew(new TBMenu 
    {
        TBMenuID=1, 
        Action="#", 
        Controller=string.Empty, 
        Icon="fui-user", 
        Nome="Cadastros", 
        Posicao=0, 
        Tooltip="Cadastros"
        Perfis = new List<TBPerfil>
        {
            this.Perfil1,
            this.Perfil2
        }
    }

context.SaveChanges();            
    
24.06.2016 / 22:30