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

Possibility to introduce templating for on-model-creating method #695

Closed
mbharodia opened this issue Apr 12, 2018 · 11 comments
Closed

Possibility to introduce templating for on-model-creating method #695

mbharodia opened this issue Apr 12, 2018 · 11 comments

Comments

@mbharodia
Copy link

mbharodia commented Apr 12, 2018

Here is handle bar template for DbContext

{{> dbimports}}
namespace {{namespace}}
{
public partial class {{class}} : DbContext
{
{{> dbsets}}
{{#if entity-type-errors}}
{{#each entity-type-errors}}
{{spaces 8}}{{{entity-type-error}}}
{{/each}}
{{/if}}
{{{on-configuring}}}
{{{on-model-creating}}}
}
}

on-model-creating injects

protected override void OnModelCreating(ModelBuilder modelBuilder)

I like to do the following

protected override void OnModelCreating(ModelBuilder modelBuilder)
{

  `//code generated by the tool goes here//`

  `//my own code that I could add using template//`
  `OnModelCreatingPartial(modelBuilder);`

}

The way template has been designed right now, I am not able to inject the following (or I may not know)

OnModelCreatingPartial(modelBuilder)

Here is my updated template to introduce partial method

{{> dbimports}}
namespace {{namespace}}
{
public partial class {{class}} : DbContext
{
{{> dbsets}}
{{#if entity-type-errors}}
{{#each entity-type-errors}}
{{spaces 8}}{{{entity-type-error}}}
{{/each}}
{{/if}}
{{{on-configuring}}}
{{{on-model-creating}}}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
}

As DbContext is partial class already, I can have another partial class and add required code as per my requirements. I think this provides me an easier way to extend the functionality using PARTIAL setup.

I think the above is already available in EF6 POCO Generated and it has been useful to me in many instances. I am just wondering whether there is any possibility of doing the same thing.

I highly appreciate your recommendation or feedback or any help in this regard

@ErikEJ
Copy link
Owner

ErikEJ commented Apr 12, 2018

PRs are very welcome - does this work for you as desired?

@ErikEJ
Copy link
Owner

ErikEJ commented Apr 12, 2018

Looks like it is related to #683

@mbharodia
Copy link
Author

mbharodia commented Apr 12, 2018 via email

@ErikEJ
Copy link
Owner

ErikEJ commented Apr 15, 2018

I will try to simply always add the two extra lines:

   OnModelCreatingPartial(modelBuilder);

and

  partial void OnModelCreatingPartial(ModelBuilder modelBuilder);

@mbharodia
Copy link
Author

mbharodia commented Apr 15, 2018 via email

@ErikEJ
Copy link
Owner

ErikEJ commented Apr 28, 2018

@mbharodia Fixed in latest daily build (no Handlebars required)

@ErikEJ ErikEJ closed this as completed Apr 28, 2018
@mbharodia
Copy link
Author

mbharodia commented Apr 28, 2018 via email

@ErikEJ
Copy link
Owner

ErikEJ commented Apr 28, 2018

Please let me know if it solves your issue!

@mbharodia
Copy link
Author

Hi Erik:

It looks like that it is not working as per the expectation. When I got the latest daily build, it generated the following code.

namespace CCData.DataContext
{
    public partial class CustomCareDataContext : DbContext
    {
        public virtual DbSet<Account> Accounts { get; set; }       

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Account>(entity =>
            {
                entity.ToTable("accounts");

                entity.Property(e => e.AccountId).HasColumnName("account_id");

                entity.Property(e => e.AccountIsVerified)
                    .IsRequired()
                    .HasColumnName("account_is_verified")
                    .HasMaxLength(3)
                    .IsUnicode(false)
                    .HasDefaultValueSql("('NRQ')");

                entity.Property(e => e.AccountOpeningBalance)
                    .HasColumnName("account_opening_balance")
                    .HasColumnType("numeric(8, 2)");

                entity.Property(e => e.AccountOpeningDate)
                    .HasColumnName("account_opening_date")
                    .HasColumnType("datetime");

                entity.Property(e => e.AccountStatus)
                    .IsRequired()
                    .HasColumnName("account_status")
                    .HasMaxLength(3)
                    .IsUnicode(false)
                    .HasDefaultValueSql("('ACT')");

                entity.Property(e => e.CompanyId).HasColumnName("company_id");

                entity.HasOne(d => d.Company)
                    .WithMany(p => p.Accounts)
                    .HasForeignKey(d => d.CompanyId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_accounts_companies");
            });
            
        }

    }
            OnModelCreatingPartial(modelBuilder);
}

I think that the correct code should be like the following. Please pay attention to "OnModelCreatingPartial" at two different places below.

namespace CCData.DataContext
{
    public partial class CustomCareDataContext : DbContext
    {
        public virtual DbSet<Account> Accounts { get; set; }
       

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Account>(entity =>
            {
                entity.ToTable("accounts");

                entity.Property(e => e.AccountId).HasColumnName("account_id");

                entity.Property(e => e.AccountIsVerified)
                    .IsRequired()
                    .HasColumnName("account_is_verified")
                    .HasMaxLength(3)
                    .IsUnicode(false)
                    .HasDefaultValueSql("('NRQ')");

                entity.Property(e => e.AccountOpeningBalance)
                    .HasColumnName("account_opening_balance")
                    .HasColumnType("numeric(8, 2)");

                entity.Property(e => e.AccountOpeningDate)
                    .HasColumnName("account_opening_date")
                    .HasColumnType("datetime");

                entity.Property(e => e.AccountStatus)
                    .IsRequired()
                    .HasColumnName("account_status")
                    .HasMaxLength(3)
                    .IsUnicode(false)
                    .HasDefaultValueSql("('ACT')");

                entity.Property(e => e.CompanyId).HasColumnName("company_id");

                entity.HasOne(d => d.Company)
                    .WithMany(p => p.Accounts)
                    .HasForeignKey(d => d.CompanyId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_accounts_companies");
            });

            OnModelCreatingPartial(modelBuilder);
        }

        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);

    }
            
}

Additionally, handlebar setup stopped working. I know that you have mentioned that this build is for "no handlebar" support. I guess it is still good to have handlebar to do some customization if needed.

The above explanation may not make sense if I have missed anything that you wanted me to look into before running the templates for your latest daily build. Please let me know your feedback.

@ErikEJ
Copy link
Owner

ErikEJ commented Apr 30, 2018

Hmmm... Tried a diferent approach in the latest build

@ErikEJ
Copy link
Owner

ErikEJ commented May 19, 2018

This issue was moved to ErikEJ/EFCorePowerTools#20

@ErikEJ ErikEJ closed this as completed May 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants