我试图在MySQL中使用Entity Framework。我刚刚安装了MySQL Installer for Windows,其中包括MySQL Connector for .NET。
我找不到任何关于如何使用这个东西的文档,所以我从各种教程中拼凑起来。我需要MySql.Data
和MySql.Data.Entity for EF6
(screenshot)的参考资料。
然后我从some random project偷了这个源代码,这是我能找到MySqlConnectionFactory
的唯一参考:
class ImgSigDbConfig : DbConfiguration
{
public ImgSigDbConfig()
{
base.AddDependencyResolver(new MySqlDependencyResolver());
base.SetProviderFactory(MySqlProviderInvariantName.ProviderName, new MySqlClientFactory());
base.SetProviderServices(MySqlProviderInvariantName.ProviderName, new MySqlProviderServices());
base.SetDefaultConnectionFactory(new MySqlConnectionFactory());
base.SetMigrationSqlGenerator(MySqlProviderInvariantName.ProviderName, () => new MySqlMigrationSqlGenerator());
base.SetProviderFactoryResolver(new MySqlProviderFactoryResolver());
base.SetManifestTokenResolver(new MySqlManifestTokenResolver());
}
}
我无法弄清楚如何在那里设置我的数据库凭据,但apparently您可以在构建DbContext
时传递它们:
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<ImgSimContext>());
var conn = new MySqlConnectionStringBuilder
{
Server = "localhost",
UserID = "root",
Password = "pass",
Database = "ImgSig"
};
using (var db = new ImgSimContext(conn.ToString()))
{
SqlConnection.ClearAllPools();
db.Database.Initialize(force: true);
现在我创建了一个非常简单的POCO:
internal class ImgSig
{
[Key, Required]
public int Id { get; set; }
[Index, StringLength(16), Required]
public byte[] Hash { get; set; }
}
但是当它命中db.Database.Initialize
时会抛出异常:
BLOB / TEXT专栏&#39; Hash&#39;在没有密钥长度的密钥规范中使用
这很有道理,因为在MySQL中你必须为任何blob / text字段设置密钥长度(尽管看看它试图运行的SQL会很好。)
所以我的问题是:
Hash
的数据类型更改为固定长度binary(16)
?