@echo off
setlocal disableDelayedExpansion

:Variables
set InputFile=%2
set OutputFile=%InputFile%#
set entity=%1
set "_strFind=OnModelCreating(ModelBuilder modelBuilder)"
set "_strInsert1=        public virtual DbSet<%entity%> %entity%Set { get; set; }"
set "_strInsert2=            modelBuilder.Entity<%entity%>(entity =>"
set "_strInsert3=            {"
set "_strInsert4=                entity.ToView("%entity%", "webapi");"
set "_strInsert5=            });"
set doreplace=0

rem if already inserted, do nothing
:Check
for /F "delims=" %%L in ('findstr /n "^" "%InputFile%"') do (
  set "line=%%L"
  setlocal EnableDelayedExpansion

  set "line=!line:*:=!"
  if "!line!" neq "" (
    set "_line=!line:%_strInsert1%=!"
  ) else (
    set _line=!line!
  )
  if "!_line!" neq "!line!" goto finish
  endlocal
)

:Replace
>"%OutputFile%" (
  for /F "delims=" %%L in ('findstr /n "^" "%InputFile%"') do (
    set "line=%%L"
    setlocal EnableDelayedExpansion

    set "line=!line:*:=!"
    if "!line!" neq "" (
      set "_line=!line:%_strFind%=!"
    ) else (
      set _line=!line!
    )


    if "!_line!" neq "!line!" (
           echo !_strInsert1!
           echo.!line!
           endlocal
           set doreplace=1
    ) else (
           echo.!line!
           if !doreplace!==1 (
             echo !_strInsert2!
             echo !_strInsert3!
             echo !_strInsert4!
             echo !_strInsert5!
             echo. 
           )
       endlocal
       set doreplace=0
    )
  )
)

del %InputFile%
copy %OutputFile% %InputFile%>nul
del %OutputFile%
:finish