Run entityframework cmdlets from my code - entity-framework-4.3

I am creating a small program over entityframework which allows to edit the POCOs with a UI. as part of the process i would like to call the "add-migration" command from my code to save the interaction of the rest of the programmers with the program manager console. is it possible at all?
thanks

Add-Migration cmdlet is defined in separate EF 4.3 Powershell assembly used by Package manager console. This assembly references a real EF 4.3 assembly. The core logic exposed from that assembly is ToolingFacade class from System.Data.Entity.Migrations.Design namespace. The exposed logic involves retrieving database and pending migrations and scaffolding a new migration but PowerShell assembly contains the execution workflow and creates bridge between EF, PowerShell and Visual studio (adding classes to your project) - this is what you must reverse engineer and reimplement in your tools.
Edit: You can also try to run MigrationCommands.AddMigration directly from PowerShell assembly.

Related

How to install Entity Framework of standard library

I installed EF with NuGet and the project's dependencies folder shows warning that EF may not be compatible. Which package do I install to use EF from the standard library?
I installed this EF:
Here is the assembly used in another project of the solution:
That's because you added the package for EF 6.2, which only targets the Full framework. It won't work on the .NET Core Runtime.
You need to use Entity Framework Core. This is was fully rewritten to target .NET Standard and fix many of the problems people had with the older Entity Framework.
Since it targets .NET Standard, it can be used in all runtimes - .NET Core, Full framework and UWP projects.
Its new features make it a great choice for the Full framework as well. For example, EF Core 2.2 added spatial types support, by using the open source NetTopologySuite package. Even EF 6.2 never had spatial type support.
Entity Framework Core is broken into various packages which enables you to add only the drivers/features you need to your project. Even the SQL Server provider is available as a separate package. A list of providers is available here
Luckily, each provider brings in all other required dependencies so all you need to do is include a provider to bring in all other required packages as transitive dependencies. That means, they don't appear as dependencies in Visual Studio or the csproj file. No more 50 package references that we don't know what to do about!
For SQL Server, you need to include Microsoft.EntityFrameworkCore.SqlServer, eg with
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
You need to add Microsoft.EntityFrameworkCore.Tools if you want to use database migration commands. If not, just don't add the package. You can always add it later.
Do add Microsoft.EntityFrameworkCore.Analyzers. It's a Roslyn analyzer that checks for common EF Core errors, especially when you use the RawSql method. It's way too easy to get this wrong.
Consider adding Microsoft.EntityFrameworkCore.InMemory in your test projects. It's a simple in-memory provider that can be used to test EF code without connecting to a database as shown here
Which package do I install to use EF from the standard library?
Just as Panagiotis pointed out, you should use Microsoft.EntityFrameworkCore instead of EntityFramework.
If you download those two packages from nuget.org, EntityFramework, Microsoft.EntityFrameworkCore, then open it with NuGet Package Explorer(You can get it from Microsoft Store.):
So, if you want to use EF from the standard library, you should install the nuget package Microsoft.EntityFrameworkCore.
Hope this helps.

Calling user defined functions in Package Manager Console in Visual Studio

I created a default ClassLibrary project in visual studio. Then I added an basic class that has one method in it.
Then I opened Package Manager console in visual studio. And I run standard Regex replace method on it and it works. Then I try to run my own replace method but it didnt work. Is there any way to run user defined methods on Package manager console or is it only allowed by core libraries on windows ?
PowerShell needs to load the type first before it can call it.
Add-Type -Path c:\test\ClassLibrary1.TestClass.cs
[ClassLibrary1.TestClass]::Replace("testx","x","y")
You could also put the add-type call into your NuGet_profile.ps1, and load your DLL if there will be multiple classes you need to call.

PowerShell module for handling Visual Studio Object Model from within NuGet Install.ps1/Uninstall.ps1 scripts

I'm trying to create a NuGet package that contains both managed (assemblies) and unmanaged (native DLLs) binaries, and also support several platforms (x86, x64).
To have a better understanding of how to handle that properly, I had a look at several packages that share similar requirements, published on the official nuget feed. One of them is Microsoft.SQLServer.Compact.
Seeing what is done in the Install.ps1, Uninstall.ps1 PowerShell scripts and in the VS.psm1 PowerShell module is very helpful.
The VS.psm1 module contains definitions for a few functions allowing to control Visual Studio from within the package installation script (notably, through the $dte object). These functions are based on the Visual Studio Object Model, which I don't know yet and that is (in my opinion) not very well documented.
I was wondering if you know about a PowerShell module that would allow to easily handle the Visual Studio Object model from a NuGet PowerShell script. Alternatively, any pointer to books, blog posts, articles, ... showing a few examples about how to properly use that object model from PowerShell would be very welcome. The only examples I have found on the net usually illustrate very basic functionality.
Also, if you know about other NuGet packages dealing with both managed and unmanaged libraries that I could use as examples, that would be nice.
I could of course start from what I have seen in the files mentioned above and roll my own module, but I would rather concentrate on my package itself if there is already something available.
Thanks for your help.
Take a look at StudioShell. It is a system for bringing PowerShell automation into VS.NET and is available as an installer but also as a NuGet package to enable the sort of scenario you describe. PluralSight course "Everyday PowerShell for Developers" course has an intro to the system and there are some examples on the project site.
This may not be directly related to your post. The Package Manager Console in my opinion should be called the PowerShell Console. It gives you full access to the DTE.
I have a trivial example of using it, pure PowerShell, no t4 or extensions, to add several files to Visual Studio to automate adding Command Pattern classes.
http://github.com/jefflomax/vs-package-manager-console-cmdlets
We could certainly use a more complete library of PowerShell cmdlets to handle simple Visual Studio tasks in this environment.

visual studio/installshield set up project

i have implemented successfully custom actions in my visual studio setup project to encrypt app.config file and also to capture user input through textboxes. However my project has 3rd party dll references which I want to merge. I have done merging using redgate smart assembly and after that when I build the setup project using the merged obfuscated assembly there is an error during installation( unable to get installer type for assembly error 1001).
Can you show me how to encrypt app.config and capture user input via custom dialog using Installshield
InstallerClass Custom Actions ( InstallUtil ) are very fragile and should never be used. The problem you are having is most likely that your custom action assembly has a reference to an assembly that can no longer be found on the disk since it's been merged into another assembly.
This problem would happen with InstallUtil CA's whether it's being called by a Visual Studio Setup Project or an InstallShield Basic MSI project because the problem isn't in MSI, it's in your CA.
I would reccomend you look at WiX Deployment Tools Framework (DTF). It's a far cleaner way of implementing managed code custom actions so that the CA and all of it's dependencies appears as a single native DLL to the Windows Installer. At runtime the native stub extracts all the files, runs your .NET code and marshals all of the MSI API calls between the two sides for you.
It's very clean and it can be used in Setup Projects, WiX, InstallShield and other MSI authoring tools because the output is a simple Win32 DLL with exported stdcall functions. ( Msi Type 1 Custom Action Spec )

How can I t4 scaffold from powershell.exe?

NuGet's PM console is great and all but you have to be in Visual Studio to use it. I have asked this question before without any ansewr and I find it hard to believe there is no documentation on the topic.
I have two users: User A sell shoes, User B sells cars. Each have different property needs.
If I allow them to write there needed properties/datatypes to their respective Product.cs entity, how can I use PowerShell.exe to:
Scaffold the controller, views, dbcontext and repositories?
I know I can do a MSBuild afterwards, but how is it there seems to be no way to use PowerShell from say a bat file to do the scaffolding outside VS 2010?
Any insight would be appreciated.
I finally figured this out after days of no luck. First and foremost, do not use any Microsoft betas, incl Windows 8 Developer Edition, PowerShell 3.0 and VS 2011. Once you have PowerShell 2.0 up and running:
1. in the PS/v1.0 folder add a powershell.exe.config file:
Follow this link for the script:http://connect.microsoft.com/PowerShell/feedback/details/525435/net-4-0-assemblies-and-powershell-v2
restart PowerShell 2.0 and it will now support 4.0 framework.
2. Set the execution policy to require only remote scripts to be sign:
Command line: Set-ExecutionPolicy RemoteSigned
3. Import the required Custom t4 scaffolders (so you can use T4 Scaffold):
Command Line: import-module C:\Users\Admin\Documents\"Visual Studio 2010"\Projects\MvcApplication1\packages\T4Scaffolding.1.0.5\tools\T4Scaffolding.NuGetServices.dll
Command Line: import-module C:\Users\Admin\Documents\"Visual Studio 2010"\Projects\MvcApplication1\packages\T4Scaffolding.1.0.5\tools\T4Scaffolding.dll
4. Now invoke your powershell file:
Command Line:C:\Users\Admin\Documents\"Visual Studio 2010"\Projects\MvcApplication1\MvcApplication1\CodeTemplates\Scaffolders\StevceScaffolders.AjaxGrid\StevceScaffolders.AjaxGrid.ps1
(be sure to quote any folder that has spaces, eg. "Visual Studio 2010"
I certainly think it would have been better if MS provided some insight on this in their documentation.
In trying to run it from powershell, you're in hardly-tread territory but its conceivable.
I think the T4 templates are rendered by an external tool. I can't see any reason why you couldn't invoke that EXE from powershell, passing parameters on the command line of the t4 template to be invoked. The resultant file (or files, see the Entity Framework T4 examples for how to write multiple files) would be output by the t4 conversion tool upon completion.
Your various different file types would be handled by different t4 templates, a Controller.t4, a Views.t4, Dbcontext.t4 and respositories.t4.
Hope that helps

Resources