EF 6 seamless connection with both Ms SQL and MySQL - c#

I have ASP.NET MVC application with default MS SQL 2012 data source. I need to make same app to work with MySQL db without much code change or separate compilation since both database are exact replica. I have browsed and notice following solutions :
Create separate SSDL using edmgen tool. - couldn't complete implementation example
Create separate EDMX - but how can I just switch between MsSQL and MySQL in web.config file
Applying [DbConfigurationType(typeof(MySqlEFConfiguration))] in DAL module - but this require separate compilation.
I referred following in my project :
MySQL.Data
MySQL.Data.Entity.EF6
And following is the settings in my web.config file :
<connectionStrings>
<add name="DefaultConnection" connectionString="server=localhost;user id=root;password=xxxxx;persistsecurityinfo=True;database=pscope4" providerName="MySql.Data.MySqlClient" />
<add name="DefaultConnection-SQL" connectionString="Data Source=.\SQLSERVER2012;Initial Catalog=PScope4;persist security info=True;user id=sa;password=xxxxx" providerName="System.Data.SqlClient" />
<add name="PScopeDB" connectionString="metadata=res://*/DbModel.csdl|res://*/DbModel.ssdl|res://*/DbModel.msl;provider=MySql.Data.MySqlClient;provider connection string="server=localhost;user id=root;password=xxxxx;persistsecurityinfo=True;database=pscope4"" providerName="System.Data.EntityClient" />
<add name="PScopeDB-SQL" connectionString="metadata=res://*/DbModel.csdl|res://*/DbModel.ssdl|res://*/DbModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLSERVER2012;initial catalog=PScope4;persist security info=True;user id=sa;password=xxxxx;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.8.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
I'm using Asp.Net.Identity, and its working fine with both MsSQL and MySQL. But am getting following error in my EF :
Unable to cast object of type 'MySql.Data.MySqlClient.MySqlConnection' to type 'System.Data.SqlClient.SqlConnection'.

Related

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.

Change connection string in EF from localDB to SQL Server instance

I don't want to use localdb when debugging my ASP.NET MVC project. When I used the database-first wizard to create the model, it correctly connected to my SQL Server instance. However, any time I run my project locally (F5), it's connecting to the localdb.
How do I use my SQL Server instance instead of localdb when running my project in debug locally?
I have tried the suggestions here.
web.config:
<connectionStrings>
<!--<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-hpmvc-20151014093518.mdf;Initial Catalog=aspnet-hpmvc-20151014093518;Integrated Security=True" providerName="System.Data.SqlClient" />-->
<add name="MyConnectionStringName"
connectionString="metadata=res://*/Models.MyModel.csdl|res://*/Models.MyModel.ssdl|res://*/Models.MyModel.msl;provider=System.Data.SqlClient;provider connection string="data source=MyServer\MyNamedInstance;initial catalog=MyDatabase;user id=MyUser;password=MyPassword;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient"/>
</connectionStrings>
...
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="MyConnectionStringName" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
</providers>
And DbContext:
public partial class MyEntities : DbContext
{
public MyEntities() : base("MyConnectionStringName")
{
...
}
}
And yet, when I run debug on the Index view, I'm seeing the test records in localdb and not the records in my SQL Server instance.

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.

SQL Server Compact 4.0 connection string for Entity Framework code first?

I am creating an application for Windows XP so I am stuck with .Net framework 4.0. I tried to use SQL Server Compact and EF code first but it make an error when update-database.
I wish to put the database in my code directory to deploy in customer machine.
This is my connection string:
<add name="QuanLyKhoContext"
connectionString="Data Source=MyData.sdf;Persist Security Info=False;AttachDBFileName=MyData.sdf"
providerName="System.Data.SqlClient" />
And the error:
This operation requires a connection to the 'master' database. Unable to create a connection to the 'master' database because the original database connection has been opened and credentials have been removed from the connection string. Supply an unopened connection.
In one of my projects I used SQL Server Compact 4.0 and my connection string was simple enough:
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=|DataDirectory|MyDB.sdf"
providerName="System.Data.SqlServerCe.4.0" />
</connectionStrings>
I think that you should check your providerName. I am not sure if System.Data.SqlClient is the right one.
Have you installed EntityFramework.SqlServerCompact NuGet package? Once it is installed System.Data.SqlServerCe.4.0 provider name is added and it should be used in the connection string.
Check if SqlServerCompact provider is added to your web.config.
<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" />
<provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SqlServerCe.4.0" />
<add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
</DbProviderFactories>
</system.data>
To prevent this you need to change your connection string to have:
Trusted_Connection=False;Persist Security Info=True
based on below link
http://jayhollingum.blogspot.in/2011/03/ef-codefirst-databasesetinitializser.html
After many search, I decide to move to a near approach of SQL compact, it's LocalDB. I change it and this work:
<add name="QuanLyKhoContext" connectionString="Data Source=(LocalDB)\v11.0;
AttachDbFilename=|DataDirectory|MyDB.mdf;Integrated Security=True;Connect Timeout=60" providerName="System.Data.SqlClient" />
I think "System.Data.SqlClient" is a valid Provider because I am using Sql Express LocalDB :)
It's not my first purpose, but it work anyway. Thanks all
I just don't know when deploy in window XP, do I need to install Sql Express or Sql Compact stuffs manually?
According to MSDN documentation you can use the following:
<add name="ConnectionStringName"
providerName="System.Data.SqlServerCe.4.0"
connectionString="Data Source=|DataDirectory|\DatabaseFileName.sdf" />
PS: How to install SQL Server Compact

Entity Framework 6 and MySQL, model first

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>

Categories