前面有说明使用EF动态新建数据库与表,数据库使用的是SQL SERVER2008的,在使用MYSQL的时候还是有所不同

一、添加 EntityFramework.dll ,System.Data.Entity.dll ,MySql.Data, MySql.Data.Entity.EF6

注意:Entity Framework5.0.0(好像最新的6.X是不支持mysql的)

二、添加dll文件,可以使用“管理NuGet程序包”或者“Nuget程序包管理控制台”添加

与 控制台平台添加

以下为控制台平台安装dll语句
EntityFramework.dll : Install-Package EntityFramework -Version 5.0.0
MySql.Data : Install-Package MySql.Data -Version 6.9.12
MySql.Data.Entity:Install-Package MySql.Data.Entity -Version 6.9.12

三、在app.config文件中添加connectionStrings

源文件

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
</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.12.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.12.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
<connectionStrings>
<!--<add name="conncodefirst" connectionString="server=127.0.0.1;port=3306;uid=root;pwd=repower;database=code" providerName="MySql.Data.MySqlClient"/>-->
<add name="conncodefirst" connectionString="server=127.0.0.1;user id=root;password=repower;database=code" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
</configuration>

四、添加类

[Table("Student")]
public class Student
{
public Student()
{ }
[Key]
public int StudentID { get; set; }
public string StudentName { get; set; }
public DateTime AddTime { get; set; } } [Table("Standard")]
public class Standard
{
public Standard()
{ }
public int StandardId { get; set; }
public string StandardName { get; set; }
public DateTime AddTime { get; set; } }

  

添加SchoolContext 继承DbContext

[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
public class SchoolContext : DbContext
{
public SchoolContext()
: base("name=conncodefirst")
{ } static SchoolContext()
{
DbConfiguration.SetConfiguration(new MySql.Data.Entity.MySqlEFConfiguration());
} public DbSet<Student> Students { get; set; }
public DbSet<Standard> Standards { get; set; } //protected override void OnModelCreating(DbModelBuilder modelBuilder)
//{
// base.OnModelCreating(modelBuilder);
//}
}

  

最后调用

SchoolContext dbCOntext = new SchoolContext();
//dbCOntext.Database.
bool flag = dbCOntext.Database.CreateIfNotExists();
Student stud = new Student() { StudentName = "New Student", AddTime = DateTime.Now };
dbCOntext.Students.Add(stud);
dbCOntext.SaveChanges();

  结果

C# 动态创建数据库三(MySQL)的更多相关文章

  1. ASP.NET实现二维码 ASP.Net上传文件 SQL基础语法 C# 动态创建数据库三(MySQL) Net Core 实现谷歌翻译ApI 免费版 C#发布和调试WebService ajax调用WebService实现数据库操作 C# 实体类转json数据过滤掉字段为null的字段

    ASP.NET实现二维码 using System;using System.Collections.Generic;using System.Drawing;using System.Linq;us ...

  2. java 动态创建数据库和动态连接数据库

    项目中有一个需求要动态创建数据库并且要动态连接数据库,本来以为还很难实现呢,在网上找了好久,都不是很理想,最后看到有人说创建数据库时,先连接到任意一个数据库,获得连接后用createStatement ...

  3. hibernate动态创建数据库表名几种方式

    数据库中数据量很大, 但又不可以删除时同时又要优化程序检索数据时间. 答:方式有很多比如 创建数据库表分区,创建索引, 存储过程等; 我这里采用动态创建数据库表的方式. 完全可以在不创建表分区情况下实 ...

  4. ASP.NET动态创建数据库和表

    using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; usin ...

  5. 如何创建数据库以及MySQL增删改查命令的简单运用

    在已经安装好MySQL的前提下 1.在Windows打开命令提示符窗口,输入命令启动MySQL命令行工具并登入root用户:mysql -h localhost -u root -p 2.输入密码后, ...

  6. MVC5+EF6入门教程——实现动态创建数据库与登录验证

    详细步骤 创建文件夹,规划好项目目录 创建相关实体类 (Data Model) 创建 Database Context 创建Initializer, 使用EF初始化数据库,插入测试数据 实现数据库登录 ...

  7. 使用JMeter创建数据库(Mysql)测试

    我的环境:MySQL:mysql-essential-5.1.51-win32 jdbc驱动:我已经上传到csdn上一个:http://download.csdn.net/source/3451945 ...

  8. 转:使用JMeter创建数据库(Mysql)测试

    我的环境:MySQL:mysql-essential-5.1.51-win32 jdbc驱动:我已经上传到csdn上一个:http://download.csdn.net/source/3451945 ...

  9. 【数据库】使用JMeter创建数据库(Mysql)测试

    我的环境:MySQL:mysql-essential-5.1.51-win32 jdbc驱动:我已经上传到csdn上一个:http://download.csdn.net/detail/paulwin ...

随机推荐

  1. java 内存, 类加载g

    1. java 内存区域 方法区 虚拟机栈 本地方法栈 堆 程序计数器 其中 :  方法区  和 堆 是所有线程共享的 , 其他是线程隔离的 1.  程序计数器 : 可以看做是当前线程所执行的字节码的 ...

  2. 【UI测试】--多窗口&系统资源

  3. ViewPager源码分析——滑动切换页面处理过程

    上周客户反馈Contacts快速滑动界面切换tab有明显卡顿,让优化. 自己验证又没发现卡顿现象,但总得给客户一个技术性的回复,于是看了一下ViewPager源码中处理滑动切换tab的过程. View ...

  4. codeforces C. Functions again

    题意:给定了一个公式,让你找到一对(l,r),求解出公式给定的F值. 当时没有想到,我把(-1)^(i-l)看成(-1)^i,然后思路就完全错了.其实这道题是个简单的dp+最长连续子序列. O(n)求 ...

  5. Mac osx 启用和关闭root用户

    设置->群组和用户->点击小

  6. 【转】shell expect spawn、linux expect 用法小记 看着舒服点

    使用expect实现自动登录的脚本,网上有很多,可是都没有一个明白的说明,初学者一般都是照抄.收藏.可是为什么要这么写却不知其然.本文用一个最短的例子说明脚本的原理. 脚本代码如下: ######## ...

  7. 《从0到1》深度阅读笔记zz

    没有人能精准地预测未来,我们只知道两件事:一是世界必然会变得不同:二是现在再好的描述也不能让我们看到清晰的未来. 创业者把成就归功于商业模式和机会窗口,归功于创业者本人拥有的资源和能力,但还有一个最重 ...

  8. (3)The critical role librarians play in the opioid crisis

    https://www.ted.com/talks/chera_kowalski_the_critical_role_librarians_play_in_the_opioid_crisis 00:1 ...

  9. 容器监控告警方案(cAdvisor + nodeExporter + alertmanager + prometheus +grafana)

    一.prometheus基本架构 Prometheus 是一套开源的系统监控报警框架.它启发于 Google 的 borgmon 监控系统,由工作在 SoundCloud 的 google 前员工在 ...

  10. math.net 拟合

    参考:http://blog.csdn.net/ztmsimon/article/details/50524392 在论坛中总看到有人在说Math.NET Iridium,查了一下,现在被整合到Mat ...