我试图使用linq2db和mysql连接到我的数据库,所以,我有一个简单的连接,如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LinqToDB;
using LinqToDB.Configuration;
using LinqToDB.DataProvider;
using MySql.Data.MySqlClient;
using twitter.Properties;
using DataModels;
namespace twitter.classes
{
class core
{
public static string DatabaseHost
{
get { return Settings.Default["databasehost"].ToString(); }
set { Settings.Default["databasehost"] = value; }
}
public static string DatabaseUser
{
get { return Settings.Default["databaseuser"].ToString(); }
set { Settings.Default["databaseuser"] = value; }
}
public static string DatabaseName
{
get { return Settings.Default["databasename"].ToString(); }
set { Settings.Default["databasename"] = value; }
}
public static string DatabasePassword
{
get { return Settings.Default["databasepass"].ToString(); }
set { Settings.Default["databasepass"] = value; }
}
public class ConnectionStringSettings : IConnectionStringSettings
{
public string ConnectionString { get; set; }
public string Name { get; set; }
public string ProviderName { get; set; }
public bool IsGlobal => false;
}
public class MySettings : ILinqToDBSettings
{
public IEnumerable<IDataProviderSettings> DataProviders
{
get { yield break; }
}
public string DefaultConfiguration => "Twitters"; // lets set your configuration as default, so you can call just new DataContext() or new DataConnection()
public string DefaultDataProvider => ProviderName.MySql; // and set default database type
private static IDataProvider GetDataProvider()
{
return new LinqToDB.DataProvider.MySql.MySqlDataProvider();
}
public IEnumerable<IConnectionStringSettings> ConnectionStrings
{
get
{
yield return
new ConnectionStringSettings
{
Name = "Twitters", // This is configuration name, you pass it to DataConnection constructor
ProviderName = ProviderName.MySql, // here we are setting database we are working with
ConnectionString = "server=" + DatabaseHost + "; database=" + DatabaseName + "" + ";user=" + DatabaseUser + "; password=" + DatabasePassword + ";",
};
}
}
}
public class DbTwitters : LinqToDB.Data.DataConnection
{
public DbTwitters() : base("Twitters") { }
public ITable<Account> Accounts { get { return GetTable<Account>(); } }
public ITable<Favorite> Favorites { get { return GetTable<Favorite>(); } }
public ITable<Retweet> Retweets { get { return GetTable<Retweet>(); } }
}
internal class MySqlDataReader
{
}
}
}
我试图在我的主程序中使用它来存储信息并连接到数据库,如下所示:
using (var db = new TwitterDB())
{
var query = from a in db.Accounts
select a;
现在,我的问题是,当我尝试连接到数据库时,我的程序会抛出一个错误,说明以下内容:
尝试为文件aspnetdb.mdf附加自动命名的数据库失败...
我的Tables.cs文件存在,如您所见;
我在配置文件中有连接:
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient" />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
如何修复此错误以便我可以连接到我的数据库?
这是我在linq2db项目中的appconfig.xml。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="linq2db" type="LinqToDB.Configuration.LinqToDBSection, linq2db" requirePermission="false" />
</configSections>
<linq2db defaultConfiguration="SQLiteInDir" />
<connectionStrings>
<add name="SQLiteInDir"
connectionString="Data Source=..\..\DataBase\TestDB.sqlite"
providerName="SQLite" /> <!--Place your ConnectionString and ProviderName here-->
</connectionStrings>
</configuration>
无需实现IConnectionStringSettings和ILinqToDBSettings。
并且您使用两个DataContext类发布代码:
哪个类名是对的?
UPD。我明白。您正在使用.Net Core。在https://github.com/linq2db/linq2db的自述文件中有:
.Net核心
.Net Core不支持System.Configuration,因此配置连接字符串应该实现ILinqToDBSettings,例如:
...(代码)
后来刚刚设定:
DataConnection.DefaultSettings = new MySettings();
你做了这个了吗?
检查app.config文件以获取连接字符串。我相信下面的标签会出现。删除它并添加MySQL的设置。
<connectionStrings>
<add name="xxxxxxxxx"
connectionString="Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|aspnetdb.mdf;"/>
<remove name="LocalSqlServer"/>
<add name="LocalSqlServer"
connectionString="Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|aspnetdb.mdf;"/>
</connectionStrings>
您的应用程序似乎正在尝试打开/附加aspnetdb.mdf,这是使用ASP.NET应用程序服务的默认数据库。此连接在Machine.config文件中定义,如下所示:
<connectionStrings>
<add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
</connectionStrings>
因此,如果在添加连接字符串之前触发clear
,则问题应该消失,因为它将删除继承的配置文件中定义的所有连接字符串,如machine.config中所示。
<connectionStrings>
<clear/>
<!--Add your connection string to MySql after this line-->
</connectionStrings>
请参阅clear
元素文档here
LinqToDB.Data.DataConnection.DefaultSettings = new MySettings();