C# 之 EF CodeFirst创建MySQL数据库
2015-02-28 11:02
239 查看
MySQL安装好了,今天跟大家交流一下怎么利用EntityFramework的CodeFirst在MySQL数据库中创建数据库
目标框架:.NET Framework 4
第一步:新建一个项目,然后添加如下的引用,这些引用可以在NuGet中添加,也可以到官网中下载然后添加
第二步:在配置文件中添加数据库节点配置
<span style="font-family:Arial;font-size:10px;"><?xml version="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<connectionStrings>
<add name="conncodefirst" connectionString="server=192.168.24.184;port=3306;uid=root;pwd=123456;database=code" providerName="MySql.Data.MySqlClient"/>
</connectionStrings>
</configuration></span>
第三步:我们编写实体类
<span style="font-family:Arial;font-size:10px;">using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
namespace codeFIrst4
{
//顾客类
public class customer
{
[Key]
//顾客id
public string id { get; set; }
//顾客姓名
public string cusName { get; set; }
}
}</span>
第四步:编写数据库上下文
<span style="font-family:Arial;font-size:10px;">using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
namespace codeFIrst4
{
//数据库上下文
public class HotelDBContext:DbContext
{
public HotelDBContext()
: base("name=conncodefirst")
{
}
public DbSet<customer> Customer { get; set; }
}
}</span>
第五步:编写创建数据库代码
<span style="font-family:Arial;font-size:10px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace codeFIrst4
{
class Program
{
static void Main(string[] args)
{
HotelDBContext dbCOntext = new HotelDBContext();
bool flag=dbCOntext.Database.CreateIfNotExists();
if (flag==true)
{
Console.WriteLine("MySQL数据库创建成功!");
}
else
{
Console.WriteLine("MySQL数据库创建失败!");
}
}
}
}</span>
通过上面简单的交流,我想大家都应该明白怎么用CodeFirst来创建一个MySQL数据库了,一开始学习EF的时候,我们都是在SQL Server数据库中建立映射,那么MySQL数据库和SQL Server数据库的区别在哪里呢?第一点:引用不同,MySQL需要引用MySql.Data;第二点:配置文件中的数据库节点配置不同。除了以上的两点,其他地方都差不多。
目标框架:.NET Framework 4
第一步:新建一个项目,然后添加如下的引用,这些引用可以在NuGet中添加,也可以到官网中下载然后添加
第二步:在配置文件中添加数据库节点配置
<span style="font-family:Arial;font-size:10px;"><?xml version="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<connectionStrings>
<add name="conncodefirst" connectionString="server=192.168.24.184;port=3306;uid=root;pwd=123456;database=code" providerName="MySql.Data.MySqlClient"/>
</connectionStrings>
</configuration></span>
第三步:我们编写实体类
<span style="font-family:Arial;font-size:10px;">using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
namespace codeFIrst4
{
//顾客类
public class customer
{
[Key]
//顾客id
public string id { get; set; }
//顾客姓名
public string cusName { get; set; }
}
}</span>
第四步:编写数据库上下文
<span style="font-family:Arial;font-size:10px;">using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
namespace codeFIrst4
{
//数据库上下文
public class HotelDBContext:DbContext
{
public HotelDBContext()
: base("name=conncodefirst")
{
}
public DbSet<customer> Customer { get; set; }
}
}</span>
第五步:编写创建数据库代码
<span style="font-family:Arial;font-size:10px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace codeFIrst4
{
class Program
{
static void Main(string[] args)
{
HotelDBContext dbCOntext = new HotelDBContext();
bool flag=dbCOntext.Database.CreateIfNotExists();
if (flag==true)
{
Console.WriteLine("MySQL数据库创建成功!");
}
else
{
Console.WriteLine("MySQL数据库创建失败!");
}
}
}
}</span>
通过上面简单的交流,我想大家都应该明白怎么用CodeFirst来创建一个MySQL数据库了,一开始学习EF的时候,我们都是在SQL Server数据库中建立映射,那么MySQL数据库和SQL Server数据库的区别在哪里呢?第一点:引用不同,MySQL需要引用MySql.Data;第二点:配置文件中的数据库节点配置不同。除了以上的两点,其他地方都差不多。
相关文章推荐
- C# CodeFirst(EF框架)代码优先创建数据库
- 【步步为营 Entity Framework+Reporting service开发】-(2) Code First ,EF创建数据库,数据表
- EF Code First学习笔记:数据库创建
- EF Code First 控制数据库创建
- C# EF Code First Migrations数据库迁移
- EF Code First学习笔记:数据库创建
- EF Code First学习笔记:数据库创建
- Contoso 大学 - 使用 EF Code First 创建 MVC 应用
- 8天掌握EF的Code First开发系列之3 管理数据库创建,填充种子数据以及LINQ操作详解
- EF三种编程方式详细图文教程(C#+EF)之Code First
- EF Code First学习笔记:数据库创建
- Contoso 大学 - 使用 EF Code First 创建 MVC 应用
- EF学习之CodeFirst(一)--创建Model
- EF CodeFirst学习笔记003--如何创建表
- EF CodeFirst 创建数据库
- Contoso 大学 - 使用 EF Code First 创建 MVC 应用
- EF Code First学习笔记:数据库创建(转)
- EF Code First 创建简单Demo
- EF Code First学习笔记:数据库创建(转载修改)
- EF框架之CodeFirst创建数据库