如果使用EntityFramework6链接Mysql
web.config文件中加入这些:

<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>
<connectionStrings>
<add name="MyContext" connectionString="Data Source=localhost;port=3306;Initial Catalog=MyBook;user id=root;password=123456;" providerName="MySql.Data.MySqlClient" />
</connectionStrings>

新建User类

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web; namespace CodeFirstMysql
{
public class User
{
public int Id { get; set; }
public string UserName { get; set; }
//默认string映射到mysql里是longtext类型的,加长度之后就变成varchar了
[MaxLength(30)]
public string PassWord { get; set; }
}
}

新建MyContext类,此类继承DbContext

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text; namespace CodeFirstMysql
{
public class MyContext : DbContext
{
public MyContext()
: base("name=MyContext")//web.config中connectionstring的名字
{
} public DbSet<User> Users { get; set; }
}
}

Default.aspx.cs文件内容:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; namespace CodeFirstMysql
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
InitData();
} private void InitData()
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MyContext>());
var context = new MyContext();
//插入一行值
context.Users.Add(new User {UserName = "EF6-MySQL-Code-First"});
context.SaveChanges();
}
}
}

运行之后看效果:
show tables:

desc table:

表中数据:

如果使用EntityFramework6链接Mysql的更多相关文章
- PDO链接mysql学习笔记
<?php //PDO链接mysql//dsn三种写法: //dsn01 $dsn = 'mysql:host=localhost;dbname=mysql'; //$dsn = 'mysql: ...
- Mssql链接mysql数据库
最近在做mysql数据库实时同步到mssql数据库的方案,花了一周时间,测试通过了,在实际机器上测试出现了若干问题.第一个问题就是在mssql上链接mysql的问题. 第一步,安装 Mysql ODB ...
- 如何用DOS 链接mysql
1.Ctrl+R 打开DOS窗口 2.键入 cd\ 回车进入C盘根目录 3.进入mysql bin目录下 操作mysql命令 4.输入连接数据库命令 mysql -hlocalhost -uroot ...
- SQL Server链接MySQL实践
最近在访问多数据库的时候进行了SQLServer链接MySQL数据的实践,现总结如下: 一. 安装mysql-connector-odbc驱动: 1. 在SQL Server服务器的机器上安装mys ...
- Python学习笔记9-Python 链接MySql数据库
Python 链接MySql数据库,方法很简单: 首先需要先 安装一个MySql链接插件:MySQL-python-1.2.3.win-amd64-py2.7.exe 下载地址:http://dev. ...
- Tomcat通过JNDI方式链接MySql数据库
原文:Tomcat通过JNDI方式链接MySql数据库 拷贝MySQL的JDBC驱动到Tomcat的lib路径下 配置全局数据源或者单个Web应用的局部数据源 局部数据源 在Tomcat的conf/C ...
- visual studio 2012 链接Mysql 5.1
首先在nuGet 下载MySql.Data.Entity 安装 mysql for visual studio http://www.mysql.com/why-mysql/windows/visua ...
- C# 链接MySql数据库
C# 链接MySql数据库只得注意的几点: 1.C#链接MySql数据库要在网上下载一个mysql-connector-net-6.0.4-noinstall.rar 这里面放的都是一堆dll .将 ...
- python链接mysql
1.安装MySQLdb MySQLdb 是用于Python链接Mysql数据库的接口,它实现了 Python 数据库 API 规范 V2.0,基于 MySQL C API 上建立的. 下载地址: ht ...
随机推荐
- 如何根据搜索页面内容得到的结果生成该元素的xpath路径
如何根据搜索页面内容得到的结果生成该元素的xpath路径?
- 纯正商业级小程序开发(完结版).txt
链接: https://pan.baidu.com/s/1LzlDslKxSUy3UV9o1aDKhg 提取码: sq7e 文章来源:刘俊涛的博客 欢迎关注,有问题一起学习欢迎留言.评论
- V-rep学习笔记:Reflexxes Motion Library 3
路径规划 VS 轨迹规划 轨迹规划的目的是将输入的简单任务描述变为详细的运动轨迹描述.注意轨迹和路径的区别:Trajectory refers to a time history of positio ...
- 友盟消息push功能
友盟地址:https://i.umeng.com/user/products 一.android 1.产品->U-App/U-push->立即使用->管理->左侧-集成测试-& ...
- logstash写日志elaticsearch不响应
在大量的解析日志并写入elasticsearch,在后端节点数据数量及磁盘性能等影响下,es不响应 问题描述: [--12T17::,][WARN ][logstash.outputs.elastic ...
- linux上源码编译安装mysql-5.6.28
在 linux 上编译安装 mysql-.tar.gz http://www.mysql.com/ mysql下载地址: http://www.mysql.com/downloads/mysql/#d ...
- 移植到windows下的iconv
This is a short memo about installing iconv on Windows host (specifically: Windows 7 SP1 x64). Iconv ...
- CentOS6.6安装S******Sockets服务端
1.查看系统 [root@localhost ~]# cat /etc/issue CentOS release 6.6 (Final) [root@localhost ~]# uname -a Li ...
- Java容器集合类的区别用法
Set,List,Map,Vector,ArrayList的区别 JAVA的容器---List,Map,Set Collection ├List │├LinkedList │├ArrayList │└ ...
- df看到的文件系统容量跟parted看到的分区容量差别较大的解决方法
下午同事在自己的开发机上遇到题目说到的问题,它看到挂在到/dev/sda磁盘分区5上的ext4文件系统的容量显著小于该分区的大小 df看到的文件系统容量: #df -h /dev/sda5 Files ...