I am trying to establish an ORM between a MySQL database and c# objects in Visual Studio
I have installed the following packages to add MySQL Database to the data sources list:
- Connector/NET 8.0.16: https://dev.mysql.com/downloads/connector/net/
- MySQL for Visual Studio 1.2.8: https://dev.mysql.com/downloads/windows/visualstudio/
following the recommendations in this post: How to connect to a MySQL Data Source in Visual Studio
I have created the data source and my code looks like this ...
Connection string in app.config:
<connectionStrings>
<add name="MyMapper.Properties.Settings.myConnectionString" connectionString="server=my-db-instance.xxxxxxxxxxxx.eu-west-1.rds.amazonaws.com;user id=my_user_id;password=my_user_password;persistsecurityinfo=True;database=my_database" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
DataContext in MyDataContext.cs:
namespace MyMapper
{
[Database]
public class MyDataContext : DataContext
{
public MyDataContext() : base(Properties.Settings.Default.myConnectionString) { }
public Table<ElementId> ElementIds;
}
}
Mapped class in ElementId.cs:
namespace MyMapper
{
[Table(Name = "ElementId")]
public class ElementId
{
[Column(Name = "IntegerValue", IsPrimaryKey = true)]
public int IntegerValue { get; set; }
}
}
Insert operation in MyController.cs:
ElementId my_element_id = new ElementId();
my_element_id.IntegerValue(88);
using (MyDataContext my_data_context = new MyDataContext())
{
my_data_context.ElementIds.InsertOnSubmit(my_element_id);
my_data_context.SubmitChanges();
}
When executing SubmitChanges() the following exception is thrown:
System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server
It seems that the problem is the connection with the database
Update answering proposed solution in Why am I getting "Cannot Connect to Server - A network-related or instance-specific error"?: I am not trying to connect to a SqlConnection but to a MySqlConnection