MySQL is a database management system from Oracle that currently supports Entity Framework through MySQL ADO.NET Connector, which is a fully-managed ADO.NET driver for MySQL. I will go through the steps in setting up MySQL with Entity Framework 6.1.+, using Visual Studio.
PREREQUISITES
- Visual Studio
Download free community version here. - MySQL Server
Download here. - ADO.NET driver for MySQL (Connector/NET)
Download here. - Microsoft Entity Framework 6.1.+
Adding Entity Framework
Download Entity Framework from NuGet in Visual Studio. This will automatically reference all the required Microsoft Entity Framework assemblies in your project.
Or
Run this command in Package Management Console to download Entity Framework.
Pm> Install-Package EntityFramework
Reference MySQL Assemblies (NB.These assemblies are available after installing MYSQL Connector for .NET)
- MySQL.Data
- MySQL.Data.Entity
Setting up the connection string in your Web.Config or App.Config
Set the connection string to your existing MySQL database
- <connectionStrings>
- <add name="SellRightDb" providerName="MySql.Data.MySqlClient"connectionString="server=localhost;userid=[your userid];password=[your password];database=sellright;persistsecurityinfo=True"/>
- </connectionStrings>
Entity Framework configuration
Configuring MySQL to use Entity Framework adds the block below to your Web.config or App.config.
- <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>
Create a simple Model class
- class Product {
- public int ProductId {
- get;
- set;
- }
- public string ProductName {
- get;
- set;
- }
- public double Price {
- get;
- set;
- }
- public int Quantity {
- get;
- set;
- }
- }
Creating Your DBContext class
- using System.Data.Entity;
- using MySql.Data.Entity;
Configuring your DBContext class
- // Code-Based Configuration and Dependency resolution
- [DbConfigurationType(typeof(MySqlEFConfiguration))]
- class SellRightContext: DbContext {
- //Add your Dbsets here
- public DbSet < Product > Products {
- get;
- set;
- }
- public SellRightContext()
- //Reference the name of your connection string
- : base("SellRightDb") {}
- }
- `
Enable Code First Migration
PM> Enable-Migrations
Add Code First Migration
PM> Add-Migrations [Give Your Migration Settings A name]
Update the database (Specify Verbose to see the SQL Query, i.e being executed).
PM> Update-Database -Verbose
NB.You should see your created product table.

mprofittameritechnet mprofittameritechnetPosted Nov 29, 2017, 8:10 PM
System.TypeLoadException: Inheritance security rules violated by type: 'MySql.Data.Entity.MySqlEFConfiguration'. Derived types must either match the security accessibility of the base type or be less accessible. at System.Reflection.CustomAttribute._CreateCaObject(RuntimeModule pModule, IRuntimeMethodInfo pCtor, Byte** ppBlob, Byte* pEndBlob, Int32* pcNamedArgs) at System.Reflection.CustomAttribute.CreateCaObject(RuntimeModule module, IRuntimeMethodInfo ctor, IntPtr& blob, IntPtr blobEnd, Int32& namedArgs) at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent) at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType type, RuntimeType caType, Boolean inherit) at System.RuntimeType.GetCustomAttributes(Type attributeType, Boolean inherit) at System.Attribute.GetCustomAttributes(MemberInfo element, Type type, Boolean inherit) at System.Reflection.CustomAttributeExtensions.GetCustomAttributes[T](MemberInfo element, Boolean inherit) at System.Data.Entity.Infrastructure.DependencyResolution.DbConfigurationFinder.TryFindConfigurationType(Assembly assemblyHint, Type contextTypeHint, IEnumerable`1 typesToSearch) at System.Data.Entity.Infrastructure.DependencyResolution.DbConfigurationManager.EnsureLoadedForAssembly(Assembly assemblyHint, Type contextTypeHint) at System.Data.Entity.Infrastructure.DependencyResolution.DbConfigurationManager.EnsureLoadedForContext(Type contextType) at System.Data.Entity.Migrations.DbMigrationsConfiguration`1..cctor() --- End of inner exception stack trace --- at System.Data.Entity.Migrations.DbMigrationsConfiguration`1..ctor() at Felon.Migrations.Configuration..ctor() in C:\GT-MP-Server\resources\Felon\Migrations\Configuration.cs:line 10 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Data.Entity.Migrations.Utilities.MigrationsConfigurationFinder.FindMigrationsConfiguration(Type contextType, String configurationTypeName, Func`2 noType, Func`3 multipleTypes, Func`3 noTypeWithName, Func`3 multipleTypesWithName) at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.FindConfiguration() at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Run() at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate) at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate) at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner) at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldInitialCreate(String language, String rootNamespace) at System.Data.Entity.Migrations.EnableMigrationsCommand.<>c__DisplayClass2.<.ctor>b__0() at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command) The type initializer for 'System.Data.Entity.Migrations.DbMigrationsConfiguration`1' threw an exception.
mprofittameritechnet mprofittameritechnetPosted Nov 29, 2017, 8:06 PM
thank you for your post. I would really like to work with Entity Framework and SQL but when I run the command Enable-Migrations I get the following error, The type initializer for 'System.Data.Entity.Migrations.DbMigrationsConfiguration`1' threw an exception. I am using the NuGet packages.
Eric LeducPosted Nov 22, 2017, 10:08 AM
I want start the codeFirst with a existing database you any how to do that for mysql?