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

T4 Templates don't work in asp.net core #368

Open
Guy--L opened this issue Feb 8, 2017 · 21 comments
Open

T4 Templates don't work in asp.net core #368

Guy--L opened this issue Feb 8, 2017 · 21 comments

Comments

@Guy--L
Copy link

Guy--L commented Feb 8, 2017

I've tried to adjust things but have not had any success with asp.net core, vs2017 rc3.

Is there a plan?

@pleb
Copy link
Member

pleb commented Feb 12, 2017

I would love to port PetaPoco to .net core, and hopefully one day I will, however, it comes down to time.

In the .net core world, the T4 schema generation would be replaced with the a new tool which would be added to the tools section of the project file. See tools

@Guy--L
Copy link
Author

Guy--L commented Feb 13, 2017

I see some posts at large regarding code generation for .net core. It sounds like you have a clear idea of your approach given the time. I have some time if you're willing to outline the approach.

@6pac
Copy link
Contributor

6pac commented May 4, 2018

Any updates on this? I've just set up a new PetaPoco project in Net Core 2, and it was pretty easy to get it working. Seems like a lot of the stuff that was prompting a rework in Core 1 has been re-added in 2.

In PetaPoco.cs (approx line 205) and PetaPoco.Core.ttinclude (approx line 349), replace:

_factory = DbProviderFactories.GetFactory(_providerName);

with:

_factory = System.Data.SqlClient.SqlClientFactory.Instance; // DbProviderFactories.GetFactory(_providerName);

Ensure System.Configuration.ConfigurationManager, System.Data.Common, System.Data.SqlClient (in my case) are installed with NuGet, and you should be able to build.

I am also getting an ugly error when building the T4 templates in VS2017. It appears that these are officially supported in VS2017, but it's choking with a SerializationException in GetCurrentProject(), on the line

EnvDTE.Project dteProject = (EnvDTE.Project)activeSolutionProjects.GetValue(0);

I can still build the T4 template if I right click and choose the 'debug T4 Template' option - that seems to work. And at times the regular build fixes itself for a while, but then later starts throwing this exception again. Pretty flaky.

Just wondering two things:

  • if we're moving away from T4 Templates, what's a good alternative? I can't find anything for design time scripting that's able to be project and db aware, without essentially rewriting the T4 function from the ground up
  • it appears PetaPoco is mainly downloaded by NuGet these days, and say AsyncPoco is dropping T4 support altogether. Does that mean that people are hand writing the templates? Are people really not using autogeneration? This is at the core of the tool for me.

@Guy--L
Copy link
Author

Guy--L commented May 4, 2018

I have T4 working in vs2017 modified for NPoco two weeks ago. I notice the same error on the first save. But usually after the second save, it generates my pocos each time. Scripty is a tool I was considering--to replace T4 files with csx script. But now that's moot.

@6pac
Copy link
Contributor

6pac commented May 4, 2018

Yeah I don't think Scripty can do the database access in the same way, or at least not without a lot of gymnastics. The T4 templates are surprisingly flexible.

@hippasus
Copy link

hi, do you want to try this tool PetaPoco.DBEntityGenerator I just built? I've ported the T4 template into this tool. It supports net core.

@6pac
Copy link
Contributor

6pac commented Jul 31, 2019

I note I have found a fairly simple mod that fixes the T4 generation, I've been doing it happily for some months now. It's a bit of a hack, I have been waiting to find the time to review it and make something more graceful.

@picasso566
Copy link

@hippasus Trying out your generator tool and looks good. I was going to start from scratch and maybe use something like
https://github.com/iQuarc/Geco I like how it uses string interpolation on .cs templates. You are in essence doing almost the same thing.
@6pac I am also looking for a solution that will work outside of Visual Studio (moving towards VS Code) so the T4 templates are unfortunately not going to work. I have used them for years and grown to depend on them, it's too bad.

@6pac
Copy link
Contributor

6pac commented Sep 15, 2020

@picasso566 Yeah, I have been using the T4 templates since SubSonic 3, but frankly I have to say they suck. They are fine if nothing changes and you leave them alone, but if you change them they are extraordinarily hard to debug if there are any problems (I end up applying changes a few lines at a time, because otherwise it's impossible to work out what broke and why).
Add to that the problems compiling them in later versions of VS, and it doesn't add up to a happy customer.
However I have modified the original templates substantially, and I am not keen to spend the time converting them all to some new format unless it is going to be well supported into the future.

@6pac
Copy link
Contributor

6pac commented Sep 16, 2020

@hippasus I'll have a look at your tool, I'm leaning towards an open source solution so I have some assurance of long term viability.

@picasso566
Copy link

All I know is, one of the big draws for me to PetaPoco (for the last 10 years in my case) was the auto generated classes and decorators in the T4 templates. One of the reasons I've been using Petapoco over Dapper or something else. They really speed things up for me.

@pleb
Copy link
Member

pleb commented Sep 16, 2020

I know people love the class generation T4 stuff. I'd like to have a good solution here, but it's not the easiest thing to solve without a fair bit of time and effort. Although I haven't used it yet, the generator from @hippasus looks awesome and seems to support all the major DBs, so it gets my vote.

@pleb
Copy link
Member

pleb commented Sep 16, 2020

If you're using netcore, don't forget EF can reverse eningeer now too. I don't mind EF Core (>= 3.1) and it's much better than EF 6. If you configure it correctly, you can get some good preformance out of it.

@iadaz
Copy link
Collaborator

iadaz commented Sep 16, 2020

Another approach I've had success with is using the "LoadTables()" function from the old version, and pushing the result of that through templates created in Razor syntax, using https://github.com/Antaris/RazorEngine. Unfortunately RazorEngine doesn't have a maintainer so is stuck in full .net framework. Razor has some quirks when used outside of its natural environment, but will at least offer up intellisense for most things.

@6pac
Copy link
Contributor

6pac commented Sep 16, 2020

@iadaz ... and the wheel comes full circle yet again! that is exactly how SubSonic 2 did it (well, it wasn't a razor engine, but it was the ASP NET rendering engine). Then we 'upgraded' to T4 in SubSonic 3 in about 2009.

@pleb
Copy link
Member

pleb commented Sep 16, 2020

I believe origin of the T4 templates PetaPoco uses is SubSonic 😂

Applogies, @6pac - whilst hunting around the issues trying to find the Tools link, I realised I never answered you in #502

🤔 I guess one path forward is Razor templates possibly using RazorLight, and making a PetaPoco T4 replacement Tool. I figure the use case would be to include a default template and allow people to reference their own. @hippasus would you be open to a PR adding this?

@6pac
Copy link
Contributor

6pac commented Sep 17, 2020

no worries. I've been way too busy for way too long. Hopefully this year I'll have time to sit down and test out the new dotNet Core features with MySql,, Sqlite and MSSQL (I'm actively using all three), and integrate them into my dev env, and assess T3 alternatives. I have some outstanding changes to feed back as well, regarding optionally removing reflection dependencies to allow PetaPoco to run on iOS under Xamarin.

@pleb
Copy link
Member

pleb commented Sep 17, 2020

, regarding optionally removing reflection dependencies to allow PetaPoco to run on iOS under Xamarin.

I've been wanting to remove this for a long time. @Shazwazza, a dev who works on Umbraco, said they removed it in favour of delegates and believe it's a pref improvement, which I tend to agree with. Even if there was a slight loss of pref (which I doubt), it's still IMO a better path, as it reduces complexity and improves usage scenarios. Ref PR 149

@6pac
Copy link
Contributor

6pac commented Sep 17, 2020

I can make a PR for what I've done. I've waited (a) because it was a quick hack to solve an unexpected roadblock and (b) to see if there were any more issues.
However it's been in production now for about six months, so it looks OK.

I noted in the issue at the time that the error is at runtime, not compile time, so putting in an #ifdef or similar is an option to keep the original code as an option if that's desirable for some scenarios. However it also means that I may not have fixed some code paths that I don't use.

@pleb
Copy link
Member

pleb commented Sep 17, 2020

If you submit a PR, I can run it across the integration tests to try find any missing edge cases

@dylanvdmerwe
Copy link

dylanvdmerwe commented Jan 2, 2023

https://github.com/mono/t4

The above tool will work, however it is being blocked because of the inclusion of EnvDTE and System.Windows.Forms.

I believe if we replace those two dependencies and use netstandard replacements, the T4 templates will work cross platform.

For example:

public EnvDTE.Project GetCurrentProject()  {

    IServiceProvider _ServiceProvider = (IServiceProvider)Host;
    if (_ServiceProvider == null)
        throw new Exception("Host property returned unexpected value (null)");

    EnvDTE.DTE dte = (EnvDTE.DTE)_ServiceProvider.GetService(typeof(EnvDTE.DTE));
    if (dte == null)
        throw new Exception("Unable to retrieve EnvDTE.DTE");

    Array activeSolutionProjects = (Array)dte.ActiveSolutionProjects;
    if (activeSolutionProjects == null)
        throw new Exception("DTE.ActiveSolutionProjects returned null");

    EnvDTE.Project dteProject = (EnvDTE.Project)activeSolutionProjects.GetValue(0);
    if (dteProject == null)
        throw new Exception("DTE.ActiveSolutionProjects[0] returned null");

    return dteProject;
}

Could be replaced with something like:
string projectName = Assembly.GetCallingAssembly().GetName().Name;

I understand this bubbles through, but all we need are names of folders and files which should be possible without EnvDTE dep.

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

7 participants