1

I'm facing an issue in my Entity-Framework where we create the data model by database-first (on Oracle). This provides us a generated .edmx. We noticed that with the Schema being in the .dll it's not possible to use this datamodel on a different schema name other then what's in the .edmx. We also figured out that when you remove the Schema name you can use your data.dll on different schemas.

My question is, is it possible to create a .bat file to loop over the text and remove all Schema="....." values?

I figured out some websites, but I don't know how to do it that the correct is being deleted

Replacing characters in a text file from Windows batch file

Snapshot of our .edmx

 <EntityContainer Name="ModelStoreContainer">
  <EntitySet Name="BS_AMBITION" EntityType="Self.BS_AMBITION" Schema="KNOWLEDGEBASE_MODEL" store:Type="Tables" />
  <EntitySet Name="BS_BRANCH" EntityType="Self.BS_BRANCH" Schema="KNOWLEDGEBASE_MODEL" store:Type="Tables" />
  <EntitySet Name="BS_GROUP" EntityType="Self.BS_GROUP" Schema="KNOWLEDGEBASE_MODEL" store:Type="Tables" />
</EntityContainer>

The result should be (without the Schema attribute):

<EntityContainer Name="ModelStoreContainer">
  <EntitySet Name="BS_AMBITION" EntityType="Self.BS_AMBITION" store:Type="Tables" />
  <EntitySet Name="BS_BRANCH" EntityType="Self.BS_BRANCH" store:Type="Tables" />
  <EntitySet Name="BS_GROUP" EntityType="Self.BS_GROUP" store:Type="Tables" />
</EntityContainer>
halfer
  • 19,824
  • 17
  • 99
  • 186
Leroy Meijer
  • 1,169
  • 17
  • 40
  • 3
    You could use a PowerShell regular expression wrapped in a batch/cmd line: `powershell -NoP -C "(Get-Content .\Old.edmx) -replace ' Schema=\""[^\""]+\""'|Set-Content .\New.edmx"` –  Oct 09 '18 at 10:14
  • @LotPings thanks this is the one that helped us most, this way we didn't have to add a extra dependency of a third library – Leroy Meijer Oct 23 '18 at 06:28

1 Answers1

0

This is an easy to achieve task with using JREPL.BAT written by Dave Benham which is a batch file / JScript hybrid to run a regular expression replace on a file using JScript.

@echo off
if not exist "*.edmx" goto :EOF
if not exist "%~dp0jrepl.bat" goto :EOF

for %%I in (*.edmx) do call "%~dp0jrepl.bat" "[\t ]*Schema=\x22.*?\x22" "" /I /F "%%I" /O -

The batch file first checks if there is any *.edmx file in current directory and immediately exits if this condition is not true, see Where does GOTO :EOF return to?

The batch file JREPL.BAT must be stored in same directory as the batch file with the code above. For that reason the batch file checks next if JREPL.BAT really exists in directory of the batch file and exits if this condition is not true.

The command FOR searches in current directory for non hidden files matching the wildcard pattern *.edmx and calls for each found EDMX file the batch file JREPL.BAT to case-insensitive replace any occurrence of [\t ]*Schema=".*?" by an empty string to delete this attribute.

The meaning of the regular expression search string is:

  • [\t ]* ... find a horizontal tab character or a space 0 or more times.
  • Schema= ... find this string case-insensitive because of option /I.
  • \x22 ... find a double quote character specified with its hexadecimal code value because a double quote within a double quoted argument string is not possible.
  • .*? ... find 0 or more characters non-greedy, i.e. stop on next " and not on last " in line.
  • \x22 ... find again " referenced with its hexadecimal code value.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /? ... explains also %~dp0 ... drive and path of argument 0 being the batch file itself.
  • echo /?
  • for /?
  • goto /?
  • if /?
  • jrepl.bat /?
Mofi
  • 46,139
  • 17
  • 80
  • 143