Question

I created a package which extended the DatabaseContext from .Net. So i tried to make my project use the new DatabaseContext when the package was installed. So i used a .pp file but it replaced all contents. (see image below)

enter image description here

But i only want to rename all the class usages of: IdentityDbContext to IdentityDbContextExtDbConfiguraion. i think i should achieve this with the powershell script but i could not find any tutorial which could help me any further on this. (I could only find file renaming examples).

Is there a way to achieve this in powershell or are there better options?

Thanks!

I tried something like:

ForEach ($item in Get-ChildItem -recurse) { 
$item.Name >> "test.txt"
#text = text + $item.Name + ", "
} 
#$path = ".\Models\IdentityModels.cs"
#$word = "IdentityDbContext<"
#$replacement = "IdentityDbContextExtDbConfiguration<"

#$text = get-content $path
#$newText =  $text -replace $word,$replacement
#$newText > $path

i want to find and replace for each .cs file in the project, how should i do this?

Was it helpful?

Solution

If you are fine replacing all instances of IdentityDbContext< with IdentityDbContextExtDbConfiguration<, you could easily achieve this using PowerShell. Just use a script like the following:

foreach ($file in Get-ChildItem -Recurse -Filter *.cs)
{
    $filename = $file.FullName; 
    $content = Get-Content $filename -Raw -Encoding UTF8
    $replacedContent = $content -replace 'IdentityDbContext<','IdentityDbContextExtDbConfiguration<' 
    $replacedContent | Set-Content $filename -Encoding UTF8
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top