DCSIMG
What does Entity Framework has to do with MSBuild? - Ido Flatow's Blog Veni Vidi Scripsi

Ido Flatow's Blog

Veni Vidi Scripsi

News

Have you heard me speak?
Powered
<style type='text/css' media='screen' id='sm_css'> #smix {overflow: visible;height: auto;border-radius: 10px;max-width: 250px;background-color: #323232;text-align: left;font-size: 12px;line-height: 16px;font-family:'Lucida Sans Unicode','Lucida Grande',Verdana,Arial,Helvetica,sans-serif;-webkit-border-radius: 10px;-moz-border-radius: 10px;border-radius: 10px;} #smix a {color: #0056CC;text-decoration: none;} #smix .sm_head {color: #fff; line-height: 1em;font-size: 1.4em;padding: 10px;color: #fff;} #smix .sm_lanyard_wrapper {background-color: #fff;;clear: both;width: 97%;margin: 0 auto;margin-bottom: 0px;} #smix .sm_lanyard_content {padding: 7px;}#smix button.sm_rec, #smix a.sm_rec, #smix input[type=submit].sm_rec { padding: 6px 10px; -webkit-border-radius: 2px 2px;-moz-border-radius: 2px; border-radius: 2px; border: solid 1px rgb(153, 153, 153); background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(rgb(255, 255, 255)), to(rgb(221, 221, 221))); color: #333; text-decoration: none; cursor: pointer; display: inline-block; text-align: center; text-shadow: 0px 1px 1px rgba(255,255,255,1); line-height: 1; }#smix .sm_rec:hover { background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(rgb(248, 248, 248)), to(rgb(221, 221, 221))); }#smix .sm_rec:active { background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(rgb(204, 204, 204)), to(rgb(221, 221, 221))); }#smix .sm_rec.medium { padding: 3px 7px; font-size: 13px; }#smix .sm_rec span.icon.thumbs_up {background-position: 0px 36px;vertical-align: text-top;display: inline-block;margin-right: 4px;height: 18px;width: 16px;background-image: url(http://speakermix.com/images/new/thumbsold.png);}#smix .sm_rec:hover span.icon.thumbs_up {background-position: 0px 18px;} #smix .sm_events {padding:2px 0px 4px 0px;} #smix .sm_section {font-size: 10px; border-bottom: 1px solid silver; margin-bottom: 6px;} #smix .sm_subline {font-size:120%;margin-top:4px;font-weight:bold} #smix .powered {text-align: right} #smix .powered img {margin: 7px} </style>
Sela Technology Center

Advertisement

What does Entity Framework has to do with MSBuild?

The answer to the above question should be “nothing, unless you’re trying to build a project that references entity framework”, but apparently it isn’t so.

Say you have a project you’ve built, and you want to build it through code, using the Engine class of Microsoft.Build.Engine assembly.

The code should look something like this (taken from MSDN):

// Instantiate a new Engine object
Engine engine = new Engine();

// Instantiate a new FileLogger to generate build log
FileLogger logger = new FileLogger();

// Set the logfile parameter to indicate the log destination
logger.Parameters = @"logfile=C:\temp\build.log";

// Register the logger with the engine

engine.RegisterLogger(logger);

// Build a project file
bool success = engine.BuildProjectFile(@"c:\somewhere\someproject.csproj");

//Unregister all loggers to close the log file
engine.UnregisterAllLoggers();

if (success)
    Console.WriteLine("Build succeeded.");
else
    Console.WriteLine(@"Build failed. View C:\temp\build.log for details");

Some notes about the code:

  1. If you refer to the sample code given in the MSDN: http://msdn.microsoft.com/en-us/library/microsoft.build.buildengine.engine.registerlogger.aspx, you can drop the line that sets the BinPath – the property is obsolete, but for some reason remained in the sample code.
  2. Don’t forget to add both Microsoft.Build.Engine and Microsoft.Build.Framework assemblies to your code (both in the 3.5 version)

When you try to run the code on a project with a target framework of 3.5, you might get an error like this in your log file ('I’ve marked the important stuff in bold):

Target EntityDeploy:
    C:\Windows\Microsoft.NET\Framework\v3.5\Microsoft.Data.Entity.targets(40,5): error MSB4127: The "EntityDeploy" task could not be instantiated from the assembly "C:\Windows\Microsoft.NET\Framework\v3.5\Microsoft.Data.Entity.Build.Tasks.dll". Please verify the task assembly has been built using the same version of the Microsoft.Build.Framework assembly as the one installed on your computer and that your host application is not missing a binding redirect for Microsoft.Build.Framework. Unable to cast object of type 'Microsoft.Data.Entity.Build.Tasks.EntityDeploy' to type 'Microsoft.Build.Framework.ITask'.
    C:\Windows\Microsoft.NET\Framework\v3.5\Microsoft.Data.Entity.targets(40,5): error MSB4060: The "EntityDeploy" task has been declared or used incorrectly, or failed during construction. Check the spelling of the task name and the assembly name.
Done building target "EntityDeploy" in project "SampleClassLibrary.csproj" -- FAILED.

If you look at the Microsoft.Data.Entity.Build.Tasks assembly, you will notice that this assembly uses both versions of Microsoft.Build.Framework – both version 2.0 and 3.5

image

Of course, if you try to add both versions to your project, it just won’t work, because both have the same name.

So, how can we make it work? just let the application know that it should always use version 3.5, even if version 2.0 is specified (implied in the error message). To do that, you need to add an assemblyBinding to your config file:

<?xml version="1.0" encoding="utf-8" ?> <configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Microsoft.Build.Framework"
publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/> <bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="3.5.0.0"/> </dependentAssembly> </assemblyBinding> </runtime> </configuration>

 

And that’s it

Comments

Greg said:

Thank you so much  This really helped me out

# January 9, 2009 11:44 PM

Blah said:

Great! Another hard coded value for some nebulous config file.

# October 12, 2009 2:27 PM

Thanks said:

Yup thanks a lot

# October 20, 2009 12:00 PM

jcollum@gmail.com said:

Good post, but I'm still having this issue. See this post if anyone else runs into this problem: social.msdn.microsoft.com/.../468d3ff5-74a9-4100-a7e4-5e71e74f57ad: .  .

Sorry for typos. This RTL stuff is acting weird.

# April 6, 2010 11:50 PM

Justin Collum said:

Thanks for the post. Unfortunately it didn't solve the same issue for me.

I posted on MS forums about it: social.msdn.microsoft.com/.../468d3ff5-74a9-4100-a7e4-5e71e74f57ad

(this may be a double post, sorry).

# April 6, 2010 11:55 PM

john@john.john said:

Thanks a lot you saved me from another headache

# July 28, 2010 3:56 PM

Scott Brooks said:

Wow, thanks for taking your time to do such a perfect article!

# November 8, 2010 12:08 AM

Adriano Gonçalves said:

Thank you so much! It worked fine for me... Remering that puting the *publicKeyToken="b03f5f7f11d50a3a"* is essential to the solution work right.

Regards

# November 10, 2010 1:18 PM

Khaniya said:

Sorry not work for me

you said there may be multiple reference of same type. But I have added only 3.5 version of framework

Note : how can I add different version of same Framework. It gives me error like "It is already exist"

Thanks

# December 3, 2010 9:05 AM

Khaniya said:

Sorry not work for me

you said there may be multiple reference of same type. But I have added only 3.5 version of framework

Note : how can I add different version of same Framework. It gives me error like "It is already exist"

Thanks

# December 3, 2010 9:05 AM

Daniel Carneiro said:

Thanks for your post. It worked like a charm

# March 2, 2011 1:06 PM

Subhash Chand said:

Thanks. It really help me.

# July 19, 2011 9:10 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: