简体   繁体   中英

Object Relational Mapping (ORM) between MySQL database and c# objects in Visual Studio using LINQ

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:

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

The data context connection was not a MySqlConnection

namespace MyMapper
{
   [Database]
   public class MyDataContext : DataContext
   {
      public MyDataContext() : base(new MySqlConnection(Properties.Settings.Default.myConnectionString)) { }
      public Table<ElementId> ElementIds;
   }
}

but now the following exception is thrown:

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '[ElementId]([IntegerValue])
VALUES (88)' at line 1

it looks that the query is not being formatted as MySQL

UPDATE: MySQL support requires NuGet Packages:

I have used MySQL.Data and Entity Framework 6 (EF6): https://dev.mysql.com/doc/connector-net/en/connector-net-entityframework60.html

The reason to choose EF6 is because EF Core does support Type Per Type ( https://entityframework.net/tpt ) at the time of writing this answer ( https://github.com/aspnet/EntityFrameworkCore/issues/2266 )

DataContext in MyDataContext.cs:

namespace MyMapper
{
   [DbConfigurationType(typeof(MySqlEFConfiguration))]
   public class MyDataContext : DbContext
   {
      public DbSet<BaseClass> BaseClass{ get; set; }
      public DbSet<ElementId> ElementId { get; set; }

      public MyDataContext () : base(new MySqlConnection(Properties.Settings.Default.myConnectionString).ConnectionString) { }

      protected override void OnModelCreating(DbModelBuilder dbModelBuilder)
      {
         dbModelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); // to avoid pluralization of the tables

         // EntityTypeConfiguration<T>
         dbModelBuilder.Configurations.Add(new BaseClassConfiguration());
         dbModelBuilder.Configurations.Add(new ElementIdConfiguration());

      }
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM