【发布时间】:2011-12-08 11:24:36
【问题描述】:
我一直在尝试让 Linq 在 Ubuntu 上与 Mysql 一起使用,使用存储库中的 Mono 版本 (2.10.5),但我只是头疼。
首先,我必须修改 MySQL 提供程序 sqlmetal.exe.config,因为默认情况下它被设置为旧的和不推荐使用的版本,我设法让它工作设置我在这台机器上安装的 MySQL 连接器(来自存储库)作为 sqlmetal 的提供者。我用 sqlmetal 为我的目标数据库生成了 DataContext,这似乎有效,但我不完全确定。
生成 DataContext 后,我在 monodevelop 上创建了一个新项目来测试它,起初我尝试使用简单的 MysqlConenction 并检查连接器是否正常工作,我必须将连接器程序集添加到项目中,但这很好,有效。 然后我尝试使用以下代码使用 DataContext 进行连接:
using System;
using System.Linq;
using System.Data.Linq;
using MySql.Data.MySqlClient;
namespace test
{
public class test
{
public static void Main (String[] args)
{
Test db = new Test (new MySqlConnection("Userid=root;database=test;server=localhost;password=password"));
foreach(var tr in db.Users)
{
Console.Write(tr.Username);
}
}
}
}
此代码失败。似乎 Linq 正在为 MySQL 生成错误的 SQL 代码,至少它抛出的异常似乎是这样说的:
Unhandled Exception: MySql.Data.MySqlClient.MySqlException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[Id], [Password], [Username]
FROM [test].[Users]' at line 1
at MySql.Data.MySqlClient.MySqlStream.ReadPacket () [0x00000] in <filename unknown>:0
at MySql.Data.MySqlClient.NativeDriver.GetResult (System.Int32& affectedRow, System.Int32& insertedId) [0x00000] in <filename unknown>:0
[ERROR] FATAL UNHANDLED EXCEPTION: MySql.Data.MySqlClient.MySqlException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[Id], [Password], [Username]
FROM [test].[Users]' at line 1
at MySql.Data.MySqlClient.MySqlStream.ReadPacket () [0x00000] in <filename unknown>:0
at MySql.Data.MySqlClient.NativeDriver.GetResult (System.Int32& affectedRow, System.Int32& insertedId) [0x00000] in <filename unknown>:0
有谁知道是否可以使用 Linq 和 Mono 连接到 MySQL?我有什么遗漏吗?
谢谢!
【问题讨论】:
-
这里已经讨论过这个问题stackoverflow.com/questions/42212/…
-
我读过那个问题,但这是一个 3 年前的问题,我认为事情会改变......