Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

dotnet ef scaffold Unrecognized option '-t firstTable -t secondTable'

I have a .ps1 file that I run from PowerShell, the code is as follows:

$strTables = ""
$tables | ForEach-Object{            
    $strTables += "-t $_ "  
}
# $strTables = -t fisrtTable -t secondTable
dotnet ef dbcontext scaffold $strConn Npgsql.EntityFrameworkCore.PostgreSQL --context MyModel $strTables -v -f

If I put the variable $strTable in the command it does not recognise the -t parameter (but the variable $strConn does work)

Unrecognized option '-t fisrtTable -t secondTable'

If I write the tables without the variable, it works.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

dotnet ef dbcontext scaffold $strConn Npgsql.EntityFrameworkCore.PostgreSQL --context MyModel -t firstTable -t secondTable -v -f

I have too many tables to do this manually. Do you know how I can concatenate variable $strTables with the dotnet command?

Thanks in advance

>Solution :

If you construct a string such as -t foo and pass it via a variable to an external program, it is passed as a single, double-quoted argument (that is, donet will see "-t foo" on its command line) – and therefore won’t be recognized as parameter name-value combination.

  • You must pass -t and foo separately, as elements of an array instead.

  • When you use an array as an argument for an external program, PowerShell passes the array elements as individual, space-separated arguments:

# Create an array such as '-t', 'foo', '-t', 'bar', ...
$tableArgs = 
  $tables | ForEach-Object{            
    '-t', "$_"  
  }

# Note the use of $tableArgs as-is, which causes PowerShell to pass
# something like -t foo -t bar behind the scenes.
dotnet ef dbcontext scaffold $strConn Npgsql.EntityFrameworkCore.PostgreSQL --context MyModel $tableArgs -v -f
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading