Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
LeDucThang opened this issue Jun 23, 2022 · 1 comment
Open

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

LeDucThang opened this issue Jun 23, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@LeDucThang
Copy link

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");
       });
@LeDucThang LeDucThang added the bug Something isn't working label Jun 23, 2022
@Freddie-H
Copy link

I have the same issue when generating the DbContext. Had the look at the code generating the many to many relationships and found the issue.

In the HbsCSharpDbContextGenerator.GenerateManyToMany the property names get transformed with EntityTypeTransformationService.TransformTypeEntityName. But in this case the resolver for entitytypes shouldn't be used, but instead the resolver for a navigation property should be used (like it does in generating the one to many relationships).

Would be nice if this could be fixed so the correct code gets generated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants