有时需要在网上租用空间或数据库,mysql成本低一些,所以想将sql server转成mysql……
注意:在安装mysql时要选择文字集为utf8,否则将不能使用中文(当前也可以在创建数据库时使用utf8,不过我不知道在ef生成数据库时如何设置,希望高手指点)
一、在项目中引用mysql的ef包
通过nuget包管理器安装:entityframework6.1.3、mysql.data.entity6.9.8
也可以用nuget的命令行加入:
install-package mysql.data.entity
二、新建相关类
1、新建 user 实体类
并定义实例的字段长度,不定义的话会出现specified key was too long;max key length is 767 bytes 的错误,这是因为string 类型直接映射到mysql 中的话是longtext,而mysql 支持最大长度为767 bytes.
public class user
{
public int id { get; set; }
[stringlength(30)]
public string username { get; set; }
[maxlength(30)]
public string password { get; set; } }
2、新建 mycontext 类
并说明用mysql进行实现 [dbconfigurationtype(typeof(mysqlefconfiguration))]
[dbconfigurationtype(typeof(mysqlefconfiguration))]