Skip to content

[Bug] Wrong template for OnModelCreating with mapping tableΒ #212

@LeDucThang

Description

@LeDucThang

Describe the bug
With setting of handlebar transformer:

public void ConfigureDesignTimeServices(IServiceCollection serviceCollection)
{
    serviceCollection.AddHandlebarsScaffolding();
    serviceCollection.AddHandlebarsTransformers(
      entityFileNameTransformer: x => x + "DAO",
      entityTypeNameTransformer: x => x + "DAO",
      constructorTransformer: x =>
      {
          x.PropertyType += "DAO";
          return x;
      },
      navPropertyTransformer: x =>
      {
          x.PropertyType += "DAO";
          return x;
      });
    serviceCollection.AddSingleton<IPluralizer, Pluralizer>();
}

DbContext will be generated by reverse engine and used CodeTemplates in Handlebars.

Class of Action and Permission after generated like this:

public partial class ActionDAO
    {
        public ActionDAO()
        {
            Pages = new HashSet<PageDAO>();
            Permissions = new HashSet<PermissionDAO>();
        }

        public long Id { get; set; }
        public string Name { get; set; } = null!;
        public long MenuId { get; set; }
        public bool IsDeleted { get; set; }

        public virtual MenuDAO Menu { get; set; } = null!;
        public virtual ICollection<PageDAO> Pages { get; set; }
        public virtual ICollection<PermissionDAO> Permissions { get; set; }
    }
public partial class PageDAO
    {
        public PageDAO()
        {
            Actions = new HashSet<ActionDAO>();
        }

        public long Id { get; set; }
        public string Name { get; set; } = null!;
        public string Path { get; set; } = null!;
        public long MenuId { get; set; }
        public bool IsDeleted { get; set; }

        public virtual ICollection<ActionDAO> Actions { get; set; }
    }

The errors appear in OnModelCreating(ModelBuilder modelBuilder), in sections of mapping table

entity.HasMany(d => d.PagesDAO)
     .WithMany(p => p.ActionsDAO)
     .UsingEntity<Dictionary<string, object>>(
         "ActionPageMapping",
         l => l.HasOne<PageDAO>().WithMany().HasForeignKey("PageId").OnDelete(DeleteBehavior.ClientSetNull).HasConstraintName("FK_ActionPageMapping_Page"),
         r => r.HasOne<ActionDAO>().WithMany().HasForeignKey("ActionId").OnDelete(DeleteBehavior.ClientSetNull).HasConstraintName("FK_ActionPageMapping_Action"),
         j =>
         {
             j.HasKey("ActionId", "PageId");

             j.ToTable("ActionPageMapping", "PER");
         });

"entity.HasMany(d => d.PagesDAO)" should be "entity.HasMany(d => d.Pages)"
and ".WithMany(p => p.ActionsDAO)" should be ".WithMany(p => p.Actions)"
Expected behavior
Template should generate

entity.HasMany(d => d.Pages)
   .WithMany(p => p.Actions)
   .UsingEntity<Dictionary<string, object>>(
       "ActionPageMapping",
       l => l.HasOne<PageDAO>().WithMany().HasForeignKey("PageId").OnDelete(DeleteBehavior.ClientSetNull).HasConstraintName("FK_ActionPageMapping_Page"),
       r => r.HasOne<ActionDAO>().WithMany().HasForeignKey("ActionId").OnDelete(DeleteBehavior.ClientSetNull).HasConstraintName("FK_ActionPageMapping_Action"),
       j =>
       {
           j.HasKey("ActionId", "PageId");

           j.ToTable("ActionPageMapping", "PER");
       });

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions