title: Can't import foreign key in MySQL DB using C#, Visual Studio and MySQL.Data
body: <p>I am getting data from MSSQL DB with Entity Framework using C# and Visual Studio and importing it in MySQL DB using MySQL.Data nuget package and C# and Visual Studio again. If I have data without foreing keys everything is OK and the import is done. Here is the code:</p>
  
  <pre><code>var context = new SupermarketsChainEntities();
  var measures = context.Measures;
  var supermarkets = context.Supermarkets;
  var vendors = context.Vendors;
  var expenses = context.Expenses;
  var products = context.Products;
  
      String myConn = "server=localhost;Database=supermarkets_chain;uid=root;pwd=;";
      MySqlConnection conn = new MySqlConnection(myConn);
      conn.Open();
  
      //Insert measures
      foreach (var measure in measures)
      {
          String insertQuery = "insert into measures (id, name) values (null, @name)";
          MySqlCommand cmd = new MySqlCommand(insertQuery, conn);
          cmd.Parameters.AddWithValue("@name", measure.Name);
          cmd.ExecuteNonQuery();
      }
  </code></pre>
  
  <p>But when I try to import data in table with one-to-many relationships and foreign keys:</p>
  
  <pre><code>foreach (var product in products)
          {
              String insertQuery = "insert into products (id, name, price, measure_id, vendor_id) values (null, @name, @price, @measure_id, @vendor_id)";
              MySqlCommand cmd = new MySqlCommand(insertQuery, conn);
              cmd.Parameters.AddWithValue("@name", product.Name);
              cmd.Parameters.AddWithValue("@price", product.Price);
              cmd.Parameters.AddWithValue("@measure_id", product.Measure);
              cmd.Parameters.AddWithValue("@vendor_id", product.Vendor);
              cmd.ExecuteNonQuery();      
          }
  </code></pre>
  
  <p>something goes wrong and I get the following exception:</p>
  
  <pre><code>Unhandled Exception: MySql.Data.MySqlClient.MySqlException: Cannot add or update
   a child row: a foreign key constraint fails (`supermarkets_chain`.`products`, C
  ONSTRAINT `fk_products_measures` FOREIGN KEY (`measure_id`) REFERENCES `measures
  ` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
     at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
     at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32&amp; affectedRow, Int64&amp; i
  nsertedId)
     at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32&amp; affected
  Rows, Int64&amp; insertedId)
     at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
  
     at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
     at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior
  )
     at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
     at SqlToMysql.Program.Main(String[] args) in d:\SoftUni\DatabaseApps\Teamwork
  \SqlToMysql\SqlToMysql\Program.cs:line 67
  </code></pre>
  
  <p>Can someone help me handle the problem and import the data properly. I am attaching the DB schema <a href="http://postimg.org/image/u9h4surx3/" rel="nofollow">here</a>.</p>