Environment: EntityFramework5.0, MySql5.6, and MSSQL2012EF are powerful ORM tools. in the true sense, multi-database links refer to different types of databases and multiple databases of the same type, EF supports this well. The following is a simple demonstration: Create a basic project of MVC4.0 and Framework4.5, and then focus on WebConfig configuration.
Environment: EntityFramework5.0, MySql5.6, and MSSQL2012 EF are powerful ORM tools. in the true sense, multi-database links refer to different types of databases and multiple databases of the same type, EF supports this well. The following is a simple demonstration: Create a basic project of MVC4.0 and Framework4.5, and then focus on WebConfig configuration.
Environment: EntityFramework5.0, MySql5.6, MSSQL2012
EF is a powerful ORM tool. in the true sense, multi-database links refer to different types of databases and multiple databases of the same type. EF supports this very well, the following is a simple demonstration:
Create a basic project of MVC4.0 and Framework4.5, and then focus on WebConfig Configuration:
// No matter how many types or databases of the same type, add
"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 = canonical "connectionStringName =" DefaultConnection "enablePasswordRetrieval =" false "enablePasswordReset =" true "inline =" false "requiresUniqueEmail =" false "inline =" 5 "inline =" 6 "external = "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 = allow" 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"/>
Comment out below, otherwise it will be found by default based on the default factory
If you have other database types, you only need to add the configuration here.
In the configuration section above, the italics and red characters are modified by me. Others are automatically generated by projects.
Test:
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) {// an existing database. Otherwise, a negative number of modelBuilder. Conventions. Remove will appear.
(); Base. OnModelCreating (modelBuilder);} public DbSet
User {get; set ;}/// public's 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) {// an existing database. Otherwise, a negative number of modelBuilder. Conventions. Remove will appear.
(); Base. OnModelCreating (modelBuilder);} public DbSet
Category {get; set ;}} public class DO_Category {public DO_Category () {} [Key] public Guid Id {get; set ;}///
/// Category ///Public string Category {get; set ;}///
/// Icon ///Public string IconName {get; set ;}///
/// Sort ///Public int OrderIndex {get; set ;}///
/// Parent node ///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
(); // Operate MySql database var context = new MyContext ("DbConMySql"); context. user. add (new User {Id = Guid. newGuid (), UserName = "jackchain"}); context. saveChanges (); var userlist = context. user. toList (); // obtain the SQL Server database content var sqlc = new SQLContext ("DefaultDB"); ViewBag. clist = sqlc. category. toList (); return View (userlist );}}}
OK. The webconfig of EF6 is slightly different. Focus on webconfig Configuration
For mysql Chinese garbled characters, add the following at the end of the connection string:Character Set = utf8;