Connection String for MySQL

Nov 2, 2012 at 3:56 AM
Edited Nov 2, 2012 at 5:54 AM

How do I properly set Connection String for MySQL. I used the following code for the Configuration Setting but it's not working.

  <connectionStrings>
    <add name="PawnShopEntities" connectionString="server=localhost;database=dfs;uid=sakila;password=123654;" />
  </connectionStrings>

  <appSettings>
    <add key="PawnShopEntities.Platform" value="MySql"/>
  </appSettings>

 

 I inserted that code to my user.config and app.config. Still now working. I am sure the database name is correct, along with the user id and password. 

 

EDIT

I was able to successfully install my model design to the database. The tables and such were created. I used the following code along with the installation of the .Net MySQL provider. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Acomsys.PawnShop.EFDAL;
using System.Data.Entity;
using System.Configuration;
using Acomsys.PawnShop.EFDAL.Entity;

namespace MyBasicModel
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            string sectionName = "appSettings";
            string sectionName2 = "connectionStrings";

            System.Configuration.ConnectionStringSettings dfs = new System.Configuration.ConnectionStringSettings("PawnShopEntities", "server=localhost;database=dfs;uid=sakila;password=123654;");
            config.ConnectionStrings.ConnectionStrings.Add(dfs);
            config.AppSettings.Settings.Add("PawnShopEntities.Platform", "MySql");

            // Save the configuration file.
            config.Save(ConfigurationSaveMode.Modified);

            // Force a reload of the changed section. This  
            // makes the new values available for reading.
            ConfigurationManager.RefreshSection(sectionName);
            ConfigurationManager.RefreshSection(sectionName2);

            
            using (var context = new PawnShopEntities())
            {
                Customer df = new Customer();
                Product p = new Product();

                p.name = "SDKFJLSDF";
                p.description = "dsfsdf";
                p.cost = "sdfs";

                df.firstname = "hello";
                df.lastname = "world";
                context.AddItem(df);
                context.AddItem(p);

                context.AcceptAllChanges();
                context.SaveChanges();
                // context.Persist();
            }
        }
    }
}

 However, the data are not persisting and there are no exceptions/errors whatsoever. 

EDIT AGAIN:

Ok, somehow I got it to work with Windows Forms. The previous post was Windows Console App. However it only reads and doesn't persist.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Configuration;
using Acomsys.PawnShop.EFDAL.Entity;
using Acomsys.PawnShop.EFDAL;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            string sectionName = "appSettings";
            string sectionName2 = "connectionStrings";

            System.Configuration.ConnectionStringSettings dfs = new System.Configuration.ConnectionStringSettings("PawnShopEntities", "server=localhost;database=dfs;uid=sakila;password=123654;");
            config.ConnectionStrings.ConnectionStrings.Add(dfs);
            config.AppSettings.Settings.Add("PawnShopEntities.Platform", "MySql");

            // Save the configuration file.
            config.Save(ConfigurationSaveMode.Modified);

            // Force a reload of the changed section. This  
            // makes the new values available for reading.
            ConfigurationManager.RefreshSection(sectionName);
            ConfigurationManager.RefreshSection(sectionName2);

            Customer df = new Customer();
            Product p = new Product();

            p.name = "SDKFJLSDF";
            p.description = "dsfsdf";
            p.cost = "sdfs";
            
            df.firstname = "hello";
            df.lastname = "world";

            using (var context = new PawnShopEntities())
            {

                context.AddItem(df);
                context.AddItem(p);

                context.AcceptAllChanges();
                context.SaveChanges();
                context.Refresh(System.Data.Objects.RefreshMode.StoreWins, p);

                var productsList = context.Product.ToList();
                foreach (var item in productsList)
                {
                    Debug.WriteLine(item.name);
                }
                // context.Persist();
            }
        }
    }
}

 Any Suggestions??? This is using MySQL Database. The reads are work Ok. But the persists are not.

Nov 2, 2012 at 8:57 AM

Ok this thing is solved. I just removed the 

context.AcceptAllChanges();

it was causing the problem.

Coordinator
Nov 4, 2012 at 8:15 PM

AcceptAllChanges marks all rows as not changed so nothing is persisted to the database. You should not use this as you see. Calling context.SaveChanges will save all dirty rows to the database.