Entity Framework 6 and MySQL, model first - c#

I have a problem using Entity Framework 6 with MySQL and doing so using model/database-first technique in ASP.NET C# MVC 3.
The current situation is that I’m getting the error:
Keyword not supported.
Parameter name: metadata
The metadata is specified in the connection string in the web.config –file:
<add name="SiteNameContainer"
connectionString="metadata=res://*/Models. SiteName.csdl|
res://*/Models. SiteName.ssdl|
res://*/Models. SiteName.msl;
provider=MySql.Data.MySqlClient;
provider connection string='server=127.0.0.1;
user id=fire;password=fire_db;
database=fire_dotnet'"
providerName="MySql.Data.MySqlClient" />
I tried to remove the metadata-section in the connectionString, but then its saying that the keyword “provider” is not supported, and then “provider connection string” not supported.
I also have these 2 sections in my web.config file:
<entityFramework>
<providers>
<provider invariantName="MySql.Data.MySqlClient"
type="MySql.Data.MySqlClient.MySqlProviderServices,
MySql.Data.Entity.EF6, Version=6.8.3.0,
Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<clear />
<remove invariant="MySql.Data.MySqlClient" />
<add name="MySql.Data.MySqlClient"
invariant="MySql.Data.MySqlClient"
description=".Net Framework Data Provider for MySQL"
type="MySql.Data.MySqlClient.MySqlClientFactory,
MySql.Data, Version=6.8.3.0, Culture=neutral,
PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
I have references to MySQL Net-connector 6.8.3 files (MySql.Data, MySql.Data.Entity.EF6, MySql.Web).
My requirments are that I must use MySQL and Entity Framework 6, and the changelog for the .NET connectior says that 6.8.x added support for MySQL.
I’m out of ideas, hopefully someone can help me with this. Thanks in advance.

It seems like you may be using the wrong providerName in your connection string, it should be System.Data.EntityClient.
Here's how I finally got my model-first code to work with MySQL and what my app.config looks like:
<connectionStrings>
<add name="MyEntities"
connectionString="metadata=res://*/Data.Entity.Model.csdl|res://*/Data.Entity.Model.ssdl|res://*/Data.Entity.Model.msl;provider=MySql.Data.MySqlClient;provider connection string="Data Source=localhost;User Id=dbadmin;Password=password;Initial Catalog=database_name;""
providerName="System.Data.EntityClient"/>
</connectionStrings>
In case that doesn't work, here's the entityFramework section. You'll notice the codeConfigurationType attribute I used in the following section. It is adds dependency resolvers for MySQL, but there are other ways of doing it which you can read about in the MySQL: EF 6 Support documentation.
<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
<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>

Related

MySQL / Entity Framework / MVC

I seem to be having issues when using MySQL to connect to an existing DB. I am using Entity Framework and using it to read from a current Database.
When I go and visit a page to load info up, I get the following error
System.InvalidOperationException: 'The Entity Framework provider type 'MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity' registered in the application config file for the ADO.NET provider with invariant name 'MySql.Data.MySqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.'
My web.config:
<entityFramework>
<defaultConnectionFactory
type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6" />
<providers>
<provider invariantName="MySql.Data.MySqlClient"
type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity" />
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient" />
<add description=".Net Framework Data Provider for MySQL"
invariant="MySql.Data.MySqlClient" name="MySQL Data Provider"
type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.10.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
Help appreciated in advance

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.
I have installed EntityFramework onto MySolution.Model.
This is the app.config for Model:
<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'. Make sure the provider is registered in the 'entityFramework' section of the application config file.
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):
<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.

system.data column 'invariantname' is constrained to be unique

I'm using FirebirdSqlProvider 4.8.1.1 and EntityFramework 6.0
I get the exception system.data column 'invariantname' is constrained to be unique when I'm trying to use EntityFramework.
My app.config file:
<entityFramework>
<defaultConnectionFactory type="FirebirdSql.Data.EntityFramework6.FbConnectionFactory, EntityFramework.Firebird" />
<providers>
<provider invariantName="FirebirdSql.Data.FirebirdClient" type="FirebirdSql.Data.EntityFramework6.FbProviderServices, EntityFramework.Firebird" />
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant="FirebirdSql.Data.FirebirdClient" />
<add name="FirebirdClient Data Provider" invariant="FirebirdSql.Data.FirebirdClient" description=".NET Framework Data Provider for Firebird" type="FirebirdSql.Data.FirebirdClient.FirebirdClientFactory, FirebirdSql.Data.FirebirdClient" />
</DbProviderFactories>
</system.data>
I have only one DbProviderFactories entity in my machine.confgi file.
How to fix it?
For me there was a duplicate IBM DB2 provider.
I had to navigate to:
C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG
And then open the machine.config file and remove the duplicated line.
The 2 lines were:
<add name="IBM DB2 for i .NET Provider" invariant="IBM.Data.DB2.iSeries" description=".NET Framework Data Provider for IBM i" type="IBM.Data.DB2.iSeries.iDB2Factory, IBM.Data.DB2.iSeries, Version=12.0.0.0, Culture=neutral, PublicKeyToken=9cdb2ebfb1f93a26"/>

How to solve Server Error in '/' Application Error?

I am learning Entity Framework and trying to create a sample code using Code first approach. While running this code i am getting above mention error. I am developing code on VS 2012 and database is Microsoft SQL Server. it also shows error message as "Unable to find the requested .Net Framework Data Provider. It may not be installed."
I've developed 3 class files and one of this file is for interacting with database which throws error at:
public List<Standard> GetStandards()
{
StandardDBContext standardDBContext = new StandardDBContext();
return standardDBContext.Standard.Include("Student").ToList();
}
My web.config file:
<connectionStrings>
<add name="StandardDBContext" connectionString="server=CPC2\SQLEXPRESS; database=SchoolDatabase;User Id=sa;Password=password; integrated security=true" providerName="System.Data.SqlClient;" />
</connectionStrings>
<system.data>
<DbProviderFactories>
<add name="SqlClient Data Provider" invariant="System.Data.SqlClient" description=".Net Framework Data Provider for SqlServer" type="System.Data.SqlClient.SqlClientFactory, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</DbProviderFactories>
</system.data>
<entityFramework>
<!--<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
<parameters>
<parameter value="System.Data.SqlServerCe.4.0"/>
</parameters>
</defaultConnectionFactory>-->
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
Can any one please help me. Your help will be appreciated.

Creating an ADO.Net Entity Data Model with MySQL database?

I've created a new ASP.Net MVC Project and added references to MySql.Data, MySql.Data.Entity, MySql.Data.Entity.EF6 and MySql.Web.v20.
I've also modified the web config and added the following:
<system.data>
<Dbproviderfactories>
<clear/>
<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.6.0, Culture=neutral, PublicKeyToken=C5687FC88969C44D" />
</Dbproviderfactories>
</system.data>
<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>
However, when I try to add an ADO.Net Entity Data Model, Visual Studio doesn't pick up MySQL as a data source as shown below:
Any ideas why this is and how I could resolve this?
Thanks
I uninstalled the MySQL .Net Connector and installed the latest version.
I updated Visual Studio 2013 to Update 4.
I installed the latest MySQL for Visual Studio.
I created a new ASP.Net MVC Project and added references to MySql.Data, MySql.Data.Entity.EF6 and MySql.Web.v20.

Categories