环境:EntityFramework5.0,MySql5.6,MSSQL2012 EF是强大的ORM工具,真正意义上的多数据库链接指的是不同类型的数据库,以及同种类型的数据库多个库,EF很好的支持这一点,下面简单演示下: 创建一个MVC4.0,Framework4.5的基本项目,然后重点是WebConfig配
环境:EntityFramework5.0,MySql5.6,MSSQL2012
EF是强大的ORM工具,真正意义上的多数据库链接指的是不同类型的数据库,以及同种类型的数据库多个库,EF很好的支持这一点,下面简单演示下:
创建一个MVC4.0,Framework4.5的基本项目,然后重点是WebConfig配置:
"entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkdiv, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> //无论多少类型,多少个同类型数据库,尽管加吧 "webpages:Version" value="2.0.0.0" /> "webpages:Enabled" value="false" /> "PreserveLoginUrl" value="true" /> "ClientValidationEnabled" value="true" /> "UnobtrusiveJavaScriptEnabled" value="true" />"Forms"> namespace="System.Web.Helpers" /> namespace="System.Web.Mvc" /> namespace="System.Web.Mvc.Ajax" /> namespace="System.Web.Mvc.Html" /> namespace="System.Web.Optimization" /> namespace="System.Web.Routing" /> namespace="System.Web.WebPages" /> "DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" /> "DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" /> "DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" /> "DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" /> "ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" /> "ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" /> "ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /> "urn:schemas-microsoft-com:asm.v1"> "System.Web.Helpers" publicKeyToken="31bf3856ad364e35" /> "System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> "System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
下面这里注释掉,否则会默认根据默认工厂来找--> -->-->
如果你还有其他的数据库类型,那么只需要在这里加配置即可
以上配置部分,斜体和红字的是我修改过的,其他都是项目自动产生的。
测试:
using System; using System.Collections.Generic; using System.Configuration; using System.Data.Entity; using System.Linq; using System.Web; using System.Web.Mvc; using MySql.Data.Entity; using System.Data.Entity.ModelConfiguration.Conventions; using System.ComponentModel.DataAnnotations; namespace MvcEFMySql.Controllers { //这里是mysql的 public class MyContext : DbContext { public MyContext(string DefaultDb) : base(DefaultDb) { //Database.DefaultConnectionFactory = MySql.Data.MySqlClient.MySqlClientFactory; //Database.Connection.ConnectionString = ; Database.SetInitializer(null); } protected override void OnModelCreating(DbModelBuilder modelBuilder) { //已经存在的数据库,不然会出现负数 modelBuilder.Conventions.Remove (); base.OnModelCreating(modelBuilder); } public DbSet user { get; set; } } //这里是sqlserver的 public class SQLContext : DbContext { public SQLContext(string DefaultDb) : base(DefaultDb) { //Database.DefaultConnectionFactory = MySql.Data.MySqlClient.MySqlClientFactory; //Database.Connection.ConnectionString = ; Database.SetInitializer (null); } protected override void OnModelCreating(DbModelBuilder modelBuilder) { //已经存在的数据库,不然会出现负数 modelBuilder.Conventions.Remove (); base.OnModelCreating(modelBuilder); } public DbSet category { get; set; } } public class DO_Category { public DO_Category() { } [Key] public Guid Id { get; set; } /// /// 类目 /// public string Category { get; set; } ////// 图标 /// public string IconName { get; set; } ////// 排序 /// public int OrderIndex { get; set; } ////// 父节点 /// public Guid FatherId { get; set; } public string CreateUser { get; set; } public DateTime CreateTime { get; set; } public string ModifyUser { get; set; } public DateTime? ModifyTime { get; set; } 1438906181 public Byte[] RowVersion { get; set; } } public class User { public Guid Id { get; set; } public string UserName { get; set; } } public class HomeController : Controller { // // GET: /Home/ public ActionResult Index() { //Database.SetInitializer(new DropCreateDatabaseAlways()); //操作MySql数据库 var context = new MyContext("DbConMySql"); context.user.Add(new User { Id = Guid.NewGuid(), UserName = "jackchain" }); context.SaveChanges(); var userlist= context.user.ToList(); //获取SQLServer数据库内容 var sqlc = new SQLContext("DefaultDB"); ViewBag.clist = sqlc.category.ToList(); return View(userlist); } } }
ok尽情品味吧。EF6的webconfig稍加不同。重点还是webconfig配置
mysql中文乱码问题,请在连接串最后加:Character Set=utf8;