locked
Code First Migrations - Alpha 2 - Exception has been thrown by the target of an invocation. RRS feed

  • Question

  • I have worked through the steps mentioned in this Walkthrough. It works fine when I have a single web project in the solution.

    The solution that I am working on with the code first entity framework includes 1 - Web Project, 6 - Class Library Projects, 1 - Windows Application, and 1 - Console Application. When I try to execute the 'Add-Migration FirstMigration' command to get the migration script for the model, I am getting the following error.

    PM> Add-Migration FirstMigration

    Add-Migration : Exception has been thrown by the target of an invocation.

    At line:1 char:14

    + Add-Migration <<<<  FirstMigration

       + CategoryInfo          : NotSpecified: (:) [Add-Migration], TargetInvocationException

       + FullyQualifiedErrorId : System.Reflection.TargetInvocationException,System.Data.Entity.Migrations.AddMigrationCommand

    This was the same error that I got when I used the Update-Database along with the August 2011 CTP.

    I even tried working with the version 0.6.1.0, the error still remains even after closing and re-opening Visual Studio.

    Could you please suggest me on any work-around to get out of this error.

    Thanks,
    Devi.

    Friday, September 9, 2011 1:24 PM

Answers

  • Hi Devi,

    Thank you for sending the project through. We have released an updated NuGet package that fixes the bugs that were causing this issue:

     

    Note: If you have received the following error when trying to use Code First Migrations Alpha 2 it is most likely due to bugs that were causing a dependency on SQL Compact and a dependency on a local SQL Express instance. We have released an updated package that resolves both these issues, please ensure you have updated the EntityFramework.Migrations package to version 0.6.2. You will need to close and re-open Visual Studio after updating the package, failure to do so will result in an error stating that “The project 'Demo' does not contain any migration contexts”. If you continue to get errors after updating to the latest package and re-starting Visual Studio then please start a thread in our Pre-Release Forum.

    Add-Migration : Exception has been thrown by the target of an invocation. 
    At line:1 char:14 
    + Add-Migration <<<< Init2 
    + CategoryInfo : NotSpecified: (:) [Add-Migration], TargetInvocationException 
    + FullyQualifiedErrorId : System.Reflection.TargetInvocationException,System.Data.Entity.Migrations.AddMigrationCommand

     

     

     

    Can you confirm that the updated package resolves the issue for you.

    (Marking this reply as the answer so that it shows up first for anyone else hitting this issue. Feel free to un-mark if it doesn't fix the issue.)

    ~Rowan


    Tuesday, September 13, 2011 3:56 PM
    Moderator
  • Hey Devi,

    There are two issues you are running in to now:

    • Bug with byte[] properties - you will need to add a MaxLength attribute to Attachment.RawData and set it to 4000 or something large. This is a bug and we'll fix it for the next Alpha.
    • You have code in the constructor of your context that throws when migrations constructs the context to get the model. Can you move this code out of the constructor so that it doesn't get run when Migrations constructs your context.

    We're also adding some better error reporting which will make it easier to work out what is going wrong in situations like these.

    ~Rowan

    • Marked as answer by Devi K Tuesday, September 13, 2011 6:54 PM
    Tuesday, September 13, 2011 6:43 PM
    Moderator

All replies

  • Hi Devi,

    We have finally got to the bottom of this problem. It looks like we do have a dependency on SQLEXPRESS being on the machine in the end. We'll get another release out soon with this fixed but in the meantime there are two options:

    1. Install SQLEXPRESS - Migrations never actually creates a database on the instance, it just uses it to get some type information.
    2. Setup a SQL Server alias on your machine that redirects .\SQLEXPRESS to your instance. http://www.mssqltips.com/sqlservertip/1620/how-to-setup-and-use-a-sql-server-alias/
      Name: .\SQLEXPRESS
      Port: 1433
      Protocol: TCP/IP
      Server: <SQL Server instance you are targeting>

    Hope this helps and thank you for your patience as we worked through this.

    ~Rowan

    Friday, September 9, 2011 10:09 PM
    Moderator
  • Hi Rowan,

    Thanks a lot for the quick response.

    Unfortunately, I won't be able to try this until Monday at work. I shall try this once I am back at work and then update you.

    Thanks again..!


    Thanks, Devi.
    • Edited by Devi K Friday, September 9, 2011 10:30 PM
    Friday, September 9, 2011 10:27 PM
  • Hi Rowan,

    I do have a SQL Server Express Edition installed on my machine. I was using sql server installed on a different machine until now. I tried pointing the connection string to the sql server instance on my local machine, but that didn't work. 

    I also tried setting the database in Migrations\Settings.cs file using TargetDatabase(@"Server=.\SQLEXPRESS;Database=ECM2Test;Trusted_Connection=True");

    Either of them didn't work. Its still the same error. Any other suggestions? 

    Thanks.


    Thanks, Devi.
    • Edited by Devi K Monday, September 12, 2011 3:00 PM
    Monday, September 12, 2011 2:59 PM
  • Hi Devi,

    Any chance you can send me a project that reproduces the issue, I'm not able to reproduce the error.

    rowmil 'AT' microsoft 'DOT' com

    ~Rowan


    Monday, September 12, 2011 4:45 PM
    Moderator
  • Hi Devi,

    Thank you for sending the project through. We have released an updated NuGet package that fixes the bugs that were causing this issue:

     

    Note: If you have received the following error when trying to use Code First Migrations Alpha 2 it is most likely due to bugs that were causing a dependency on SQL Compact and a dependency on a local SQL Express instance. We have released an updated package that resolves both these issues, please ensure you have updated the EntityFramework.Migrations package to version 0.6.2. You will need to close and re-open Visual Studio after updating the package, failure to do so will result in an error stating that “The project 'Demo' does not contain any migration contexts”. If you continue to get errors after updating to the latest package and re-starting Visual Studio then please start a thread in our Pre-Release Forum.

    Add-Migration : Exception has been thrown by the target of an invocation. 
    At line:1 char:14 
    + Add-Migration <<<< Init2 
    + CategoryInfo : NotSpecified: (:) [Add-Migration], TargetInvocationException 
    + FullyQualifiedErrorId : System.Reflection.TargetInvocationException,System.Data.Entity.Migrations.AddMigrationCommand

     

     

     

    Can you confirm that the updated package resolves the issue for you.

    (Marking this reply as the answer so that it shows up first for anyone else hitting this issue. Feel free to un-mark if it doesn't fix the issue.)

    ~Rowan


    Tuesday, September 13, 2011 3:56 PM
    Moderator
  • Hi Rowan,

    Thanks for working on that. The error still remains even after updating the Migrations package to version 0.6.2.

    Thanks.


    Thanks, Devi.
    Tuesday, September 13, 2011 5:28 PM
  • Hey Devi,

    There are two issues you are running in to now:

    • Bug with byte[] properties - you will need to add a MaxLength attribute to Attachment.RawData and set it to 4000 or something large. This is a bug and we'll fix it for the next Alpha.
    • You have code in the constructor of your context that throws when migrations constructs the context to get the model. Can you move this code out of the constructor so that it doesn't get run when Migrations constructs your context.

    We're also adding some better error reporting which will make it easier to work out what is going wrong in situations like these.

    ~Rowan

    • Marked as answer by Devi K Tuesday, September 13, 2011 6:54 PM
    Tuesday, September 13, 2011 6:43 PM
    Moderator
  • Hi Rowan,

    Thanks a lot.. It works. Great work.


    Thanks, Devi.
    Tuesday, September 13, 2011 6:55 PM
  • I always get these error:

    + Add-Migration <<<<  first
        + CategoryInfo          : NotSpecified: (:) [Add-Migration], ReflectionTypeLoadException
        + FullyQualifiedErrorId : System.Reflection.ReflectionTypeLoadException,System.Data.Entity.Migrations.AddMigrationCommand


    have tried all ways metioned above.

     

    This version sucks!

    Wednesday, September 14, 2011 8:50 AM
  • TOTALLY can't believe i'm waisting my time on this...

    STOP CREATING stuff with nuget that's the fu.. problem...

    SDK companies these days suck... they release all those alpha versions but they don't understand that the installation part of any release HAVE TO BE PERFECT ALL THE TIME.. alpha, beta or not

    alpha part is for the code not the fukk.. installation

    if installation thru nuget break the code wTF...

     

    Friday, October 28, 2011 3:40 AM