Popular table with api Fluent

Asked

Viewed 95 times

1

I have this model Many to Many and need to popular the table through the file Seed as described at the end of the post using the api Fluent

inserir a descrição da imagem aqui

Context file:

public EfDbContext() : 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();            
        }
  • Could you explain what your problem is? Just in advance, the Fluent API has no connection.

  • How do I popular the table TBMenuPerfil? Just like I did with the tables TBPerfil and TBMenu using the resources that method Seed offers ?

  • Leaving the Fluent API is an option?

1 answer

1

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

Method to add and retrieve 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();            

Browser other questions tagged

You are not signed in. Login or sign up in order to post.