简体   繁体   English

使用Mysql设置实体框架6-代码优先

[英]Setup Entity Framework 6 with Mysql - Code first

I am trying to setup a existing project to use Entity Framework. 我正在尝试设置一个现有项目以使用实体框架。 I have never used it before and want to learn it on a personal project. 我以前从未使用过它,并且想在个人项目中学习它。

I have a solution with many projects, all related. 我有一个与许多项目相关的解决方案。 Login is where I wanna do querys. 登录是我想查询的地方。 Model is where the model is. 模型就是模型所在的地方。 Main is where the program starts. Main是程序启动的地方。

I have installed EntityFramework onto MySolution.Model. 我已经将EntityFramework安装到MySolution.Model上。

This is the app.config for Model: 这是Model的app.config:

<connectionStrings>
    <add name="ALDatabaseContext" providerName="MySql.Data.MySqlClient"
        connectionString="server=localhost;port=3306;database=aldatabase;uid=root;password=root"/>
</connectionStrings>
<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
<providers>
    <provider invariantName="MySql.Data.MySqlClient"
      type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"/>
     <provider invariantName="System.Data.SqlClient"
      type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
</providers>
</entityFramework>

My context is very simple 我的情况很简单

public class ALDatabaseContext : DbContext
{
    public virtual DbSet<User> Users { get; set; }
}

But when I call context from Login I get an exception: 但是,当我从登录名调用上下文时,出现异常:

Additional information: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'. 附加信息:未找到具有不变名称'System.Data.SqlClient'的ADO.NET提供程序的实体框架提供程序。 Make sure the provider is registered in the 'entityFramework' section of the application config file. 确保提供程序已在应用程序配置文件的“ entityFramework”部分中注册。

What I am missing? 我缺少什么?

Ok, I managed to get it working (but I don't like the solution). 好的,我设法使其正常工作(但我不喜欢该解决方案)。

I have added this to the app.config of Main (the entry point of my solution): 我已经将它添加到Main的app.config中(我的解决方案的入口):

<connectionStrings>
<add name="ALDatabaseContext" providerName="MySql.Data.MySqlClient" connectionString="server=localhost;port=3306;database=aldatabase;uid=root;password=root" />
</connectionStrings>



<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
  <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
</providers>


</entityFramework>


<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>

And also referenced all dlls of Model (Entity framework related dll and Mysql dlls) at Login and Main projects. 并在Login和Main项目中引用了Model的所有dll(与实体框架相关的dll和Mysql dll)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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