2

My goal is to use a prebuild script to change the schema depending on the compiler configuration (Debug/Release/Others..).

I see that there are a number of solutions to change the schema in runtime using code, I took a different approach using a prebuild script to alter the edmx file. This isn't working and I'm wondering why this isn't working (I'm not so much looking for a better/different approach.)

My approach was this: In the prebuild event I added a script to alter the edmx file changing the string Schema="dbo" to Schema="dbo2".

In the postbuild event I added a script to return the file back (Schema="dbo2" to Schema="dbo").

I also set a conditional to deal with the compiler configuration:

if $(ConfigurationName) == Release

So it looks like this:

prebuild: if $(ConfigurationName) == Release setdbo2.bat

postbuild: if $(ConfigurationName) == Release setdbo.bat

I also tried dumping the contents of the edmx file in the bat files. I am sure that the prebuild script is changing the edmx file's schema correctly and that the postbuild is changing it back correctly. The bug that I am experiencing is when I build the schema change isn't reflected in the resulting assembly (it behaves as if there was no prebuild script). I know the prebuild script is running and this can be seen from the output of the build:

1>  Completed setdbo2.bat
~~ **snip** ~~
Various stupid compiler warnings
~~ **/snip** ~~
1>  myProject -> C:\myProject\bin\myProject.dll
1>  Completed setdbo.bat

Before the snip if I dump the edmx file to the output screen I see that indeed the edmx file was altered in the manner I expected. However the myProjectDMO.ssdl file contained within the assembly still contains whatever the edmx had before the script ran.

If a completely remove the postbuild script (thinking that the ssdl might be generated/added after compilation) the ssdl is also not affected.

So it seems to me that the ssdl file is generated before the prebuild script is run.

Does anyone know if this is the case? And if so is there some sort of pre-prebuild script to deal with this?

--More information--

I found that I can set the verbosity of the output build window. From there I was able to get this:

1>------ Build started: Project: myProject, Configuration: Release Any CPU ------
1>Build started 6/4/2015 4:06:23 PM.
1>EntityDeployNonEmbeddedResources:
1>Skipping target "EntityDeployNonEmbeddedResources" because it has no outputs.
1>EntityDeployEmbeddedResources:
1>  Processing 1 EDMX files.
1>  Starting to process input file 'myProjectDMO.edmx'.
1>  Finished processing input file 'myProjectDMO.edmx'.
1>  Finished processing 1 EDMX files.
1>PreBuildEvent:
1>  if Release == Release setdbo2.bat

The edmx file does get processed before the prebuild event. The issue does not seem to be related to in-memory caching of files.

The question now becomes is there a pre-prebuild script? Or would I have to start messing with tt files or other on-code-generation events?

John Smith
  • 591
  • 4
  • 15
  • I found this article but doesn't seem to fix the problem: http://www.techartblog.com/tips/visual_studio_edit_files_prebuild – John Smith Jun 04 '15 at 17:35
  • and http://stackoverflow.com/questions/20323012/visual-studio-c-sharp-does-not-build-files-that-were-change-by-pre-build-event (which also isn't working :/ ) – John Smith Jun 04 '15 at 17:43
  • Another thing to note.. if the solution is reopened and the edmx file is never touched (never opened) the problem still persists. So this might not be a grabbing the code from memory's cache as the comments above seem to suggest – John Smith Jun 04 '15 at 22:59

1 Answers1

1

The A solution is to edit the file "obj\Release\edmxResourcesToEmbed\myProjectDMO.ssdl" instead of the edmx.

I'm sure someone on the internet can find a good reason not to do that, but this does seem to work. Visual Studios should probably build in a few more hooks to avoid hacks like this.

Under Projects > Build Events > Pre-build event command line

if $(ConfigurationName) == Release launchSED.bat

launchSED.bat launches cygwin which then executes

find ./ -type f -exec sed -i 's/dbo/NewSchema/' {} \;

That's not a great answer. Since cygwin is required. I have also built a custom sed command for windows. This program takes 3 inputs: a file path, a string to be replaced, a string to replace said string with.

winsed obj\Release\edmxResourcesToEmbed\myDMOFile.ssdl "Schema=""dbo""" "Schema=""NewSchema"""
John Smith
  • 591
  • 4
  • 15
  • Can you provide the prebuild and postbuild scripts? I want to remove schema information during build (working with Oracle). – Tasos K. Jan 14 '16 at 10:56
  • I'm using sed through cygwin you might not want it... I'll update my answer. – John Smith Jan 14 '16 at 21:14
  • Thanks for answering. You might be right about cygwin, but the scripts might be helpful to create my own. – Tasos K. Jan 15 '16 at 06:25
  • I don't mind to give you winsed if you can tell me how to give it to you. Right now it accepts 1 file and 2 strings. I had planned to allow it to accept multiple files or directories with regex pattern matching, so I don't feel like it's done right now. But your welcome to it. – John Smith Jan 16 '16 at 00:25
  • Thanks for the answer, Regarding sed, I a thinking writing something with Powershell – Tasos K. Jan 16 '16 at 08:14