Question

So, I'm confused by this failure. I'm using MSTest to run some integration tests (since there's a DB backing this, I guess we'd call it that rather than unit testing) and have a test method like so:

[TestMethod()]
[ExpectedException(typeof(DbUpdateException))]
public void AddRecipe_InvalidCookingMethod_ExpectExceptionThrown()
{
   var target = GetRepository();
   var recipe = new Recipe()
   {
      Method = "Sous-vide"
   }
   target.AddRecipe(recipe);
}

and the AddRecipe looks like this:

public void AddRecipe(Recipe recipe)
{
   // validation stuff
   // automapper stuff
   try
   {
      using (var context = GetContext())
      {
         context.recipes.Add(recipe);
         context.SaveChanges();
      }
   }
   catch (DbUpdateException ex)
   {
      Console.WriteLine(ex.ToString());
      throw;
   }
}

The underlying table has a foreign key to the cooking_method table and in this case the recipe I'm trying to insert has a cooking method that isn't in that table, so I expect a DbUpdateException to be thrown.

The test fails and states "AddRecipe_InvalidCookingMethod_ExpectExceptionThrown threw exception System.Data.Entity.Infrastructure.DbUpdateException, but exception System.Data.Entity.Infrastructure.DbUpdateException was expected...." It goes on to mention that there's an inner exception of type MySql.Data.MySqlClient.MySqlException: Cannot add or update a child row: a foreign key constraint fails..."

I'm using the MySql connector 6.4.4, entity framework 4.3, VS 2010, if any of that is useful info.

Ideas?


SOLUTION:

Thanks to Ladislav for helping out with this one. I'd moved the source tree and updated most but not all of the references. My test project was referencing the wrong version of Entity Framework.

Was it helpful?

Solution

As I pointed in comment this looks like a problem where projects in solution use different version of EntityFramework.dll library.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top