在EF中使用MySQL的方法及常见问题
有时需要在网上租用空间或数据库,Mysql成本低一些,所以想将sql server转成mysql……
一、在项目中引用mysql的EF包
Install-Package MySql.Data.Entity
二、新建相关类
public string PassWord { get; set; }
}
}
3、写测试代码
context.SaveChanges();
三、配置Web.config
完整的web.config如下:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory , EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d">
</provider>
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient" />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
<connectionStrings>
<add name="MyContext" connectionString="Data Source=localhost;port=3306;Initial Catalog=MySQL_EF;user id=root;password=root;" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
</configuration>
常见问题
- 出现错误提示: Specified key was too long;max key length is 767 bytes
1)查看实体的字符串类型属性是否设置了长度
- 出现错误提示: Model compatibility cannot be checked because the database does not contain model metadata
- 出现错误提示:序列不包含任何匹配元素
例如:1.
public class Employee
{
[Key]
public int EmployeeId { get; set; }
public string Name { get; set; } [ForeignKey("ManagerId")]
public Employee Manager { get; set; }
public int ManagerId { get; set; }
} [ForeignKey("ManagerId")]
public Employee Manager { get; set; }
public int ManagerId { get; set; }
这个外键设置。
2. [Column(TypeName="VARCHAR(254)")]
public string ColumnName { get; set; }
这样的定义,改成:
[MaxLength(254)] [Column(TypeName="VARCHAR")]
public string ColumnName { get; set; } 3.(以下代码未测试,因为我不是这样用的,在下篇文章中将进行测试)
modelBuilder.Entity<Category>()
.HasKey(c => c.IdCategory )
.HasOptional(p => p.Children)
.WithMany()
.HasForeignKey(c => c.ChildrenId);
改成:
modelBuilder.Entity<Category>()
.HasKey(c => c.IdCategory )
.HasMany(p => p.Children)
.WithOptional()
.HasForeignKey(c => c.ChildrenId); .WithMany()换成.WithOptional()
在EF中使用MySQL的方法及常见问题的更多相关文章
- 在VS的EF中连接MySQL
VS没有主动提供那些繁多的连接器,需要的话得自己再安装这些第三方程序包. MySQL为windows平台开发者提供了许多程序包:http://dev.mysql.com/downloads/windo ...
- go语言中操作mysql的方法
需要下载指定的golang的mysql驱动包 > go get github.com/go-sql-driver/mysql 下面的例子: package main; import ( &quo ...
- ADO.NET EF 中的实体修改方法
http://www.cnblogs.com/zfz15011/archive/2010/05/30/1747486.html 1.传统修改模式,看下列代码 using (NorthwindEntit ...
- EF中使用Select new 方法中字段值替换的问题
前提需要替换查询得到的List当中的某个字段的值,替换规则有一个mapping关系 尝试代码 有问题 无法获取任何数据 /// <summary> /// 获取Treegrid的List ...
- 转 ef中使用mysql步骤--Entity Framework 6 with MySql
原文:http://lvasquez.github.io/2014/11/18/EntityFramework-MySql/ For the Entity Framework 6 support we ...
- EF中的MySql返回 DataTable公共类库
public static class SqlHelper { /// <summary> /// EF SQL 语句返回 dataTable /// </summary> / ...
- Shell脚本中执行mysql的几种方式(转)
Shell脚本中执行mysql的几种方式(转) 对于自动化运维,诸如备份恢复之类的,DBA经常需要将SQL语句封装到shell脚本.本文描述了在Linux环境下mysql数据库中,shell脚本下调用 ...
- EF(EntityFramework)与mysql使用,序列化问题[System.ObjectDisposedException]
在EF 中使用mysql关联取数据时,如果当前实体中包含另一个实体的list成员,而这个成员为空的话,在json序列化的时候就会报错: '((System.Data.Entity.DynamicPro ...
- windows中Navicat连接centos中的mysql报:1130-Host '192.168.xxx.1' is not allowed to connect to this MySQL解决方法
解决方法: 在centos中登录mysql输入下面指令: grant all PRIVILEGES on *.* to ' WITH GRANT OPTION;
随机推荐
- 委托学习过程及委托、Lambda表达式和匿名方法的关系总结及事件总结
第一章,当开始学习委托的时候,我们会问什么是委托?为什么要学习委托? 一,什么是委托? 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递,这种将方法动态地赋给参数的做法, ...
- 5.mybatis实战教程(mybatis in action)之五:与spring3集成(附源码)
转自:https://blog.csdn.net/nnn9223643/article/details/41962097 在 这一系列文章中,前面讲到纯粹用mybatis 连接数据库, 然后 进行增删 ...
- MyEclipse: Java代码与UML自动转换
第一步:新建UML2 第二步:拖拽左边的代码向右侧
- gevent 实现单线程下的socket链接
通过gevent实现socket的多并发 server 端: import geventfrom gevent import socket, monkey monkey.patch_all() #进行 ...
- Mysql 主- 开启binlog
https://www.cnblogs.com/martinzhang/p/3454358.html my.cnf 添加 log_bin=mysql-bin 开启日志,然后重启mysql服务器. 查看 ...
- kafka offset 设置
from kafka import KafkaConsumer from kafka import TopicPartition from kafka.structs import OffsetAnd ...
- Unified shader model
https://en.wikipedia.org/wiki/Unified_shader_model In the field of 3D computer graphics, the Unified ...
- while and for 2
public class TestWhileAndFor2 { /** * 九九乘法表 * 1!+2!+3!+....+10!=? * */ public static void main(Strin ...
- 吴裕雄 数据挖掘与分析案例实战(8)——Logistic回归分类模型
import numpy as npimport pandas as pdimport matplotlib.pyplot as plt # 自定义绘制ks曲线的函数def plot_ks(y_tes ...
- cat 生成文件 运行脚本
nohup python -u day_std_cid_list_data_done.py >eee1.log 2>&1 & 后台运行python脚本 hadoop fs ...