惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News and Events Feed by Topic
D
Docker
云风的 BLOG
云风的 BLOG
F
Fortinet All Blogs
F
Full Disclosure
H
Hackread – Cybersecurity News, Data Breaches, AI and More
P
Proofpoint News Feed
Microsoft Azure Blog
Microsoft Azure Blog
WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
L
LangChain Blog
H
Help Net Security
B
Blog
T
Tailwind CSS Blog
V
V2EX
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
Recent Announcements
Recent Announcements
aimingoo的专栏
aimingoo的专栏
美团技术团队
A
About on SuperTechFans
C
Cybersecurity and Infrastructure Security Agency CISA
K
Kaspersky official blog
I
InfoQ
Project Zero
Project Zero
I
Intezer
Google DeepMind News
Google DeepMind News
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Threat Research - Cisco Blogs
Last Week in AI
Last Week in AI
C
Cyber Attacks, Cyber Crime and Cyber Security
G
GRAHAM CLULEY
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
AWS News Blog
AWS News Blog
Spread Privacy
Spread Privacy
S
Securelist
Recorded Future
Recorded Future
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园 - 叶小钗
S
Security Affairs
Blog — PlanetScale
Blog — PlanetScale
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
The Hacker News
The Hacker News

博客园 - meetweb

IDEA 使用generator逆向工程生成pojo,mapper Ambiguous reference to member 'dataTask(with:completionHandle:)'错误 swift3.0  代码创建经典界面的九宫图--优化篇 swift3.0 创建经典界面的九宫图 AlertDialog中使用ListView绑定数据 bootstrap table教程--后台数据绑定、特殊列处理、排序 bootstrap table教程--使用入门基本用法 jqGrid使用json实现的范例一 android logger的使用 通过反射获取DLL的类实现加载窗体 Visual 2015创建新项,缺少ADO.NET 实体数据模型的解决方法 事件使用(转 ) 使用Dotfuscator 进行.Net代码混淆 代码加密的方法 Git分布式版本控制系统学习笔记 免费SVN服务器笔记 如何设置mysql远程访问及防火墙设置 c# GridControl怎么换行 模拟Post登陆带验证码的网站 c#控制打印机杂项
在VisualStadio2015上使用EF6.0建立MySql数据库
meetweb · 2016-06-09 · via 博客园 - meetweb

1.新建工程

2.建立类的文件夹DAL

3.建立相关类

【Student类】

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ETTest3
{
public class Student
{
public int Id { get; set; }
public string LastName { get; set; }

}
}

【School类】

using System;
using System.Collections.Generic;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ETTest3
{
public class SchoolContext : DbContext
{
public SchoolContext() : base("MyContext") { }
public DbSet<Student> Students { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
}

4. 引用EF6.0 Mysql.Entity

5.调整配置文件 app.config

<connectionStrings>
<add name="MyContext" connectionString="Data Source=localhost;port=3306;Initial Catalog=test;user id=root;password=123456;" providerName="MySql.Data.MySqlClient" />
</connectionStrings>

<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>

6 测试建立数据库

try
{
SchoolContext ctx = new SchoolContext();
ctx.Database.Initialize(true);
var o = new Student();
o.Id = 1;
o.LastName = "aa";
ctx.Students.Add(o);
ctx.SaveChanges();
}
catch (Exception ex)
{
Console.Write(ex.Message);
}

7.测试成功,打开数据库,可以见到mysql里增加一个库test ,里面有一张表student

8 数据表中的字段发生变化情况处理

如果变动比较小,比如新增或删除一两个字段
先备份好数据库以后 直接进行数据表操作,将你的数据模型和数据库表做上对应就好了,
需要注意的是,项目上线一般需要将DataContext设置一下
Database.SetInitializer<DataContext>(null);
而不能设置
Database.SetInitializer<DataContext>(new DropCreateDatabaseIfModelChanges<DataContext>());或者
Database.SetInitializer<DataContext>(new DropCreateDatabaseAlways<DataContext>());等等其他方式,
以防意外导致数据库被删除,重新生成(因为自己添加字段可能与你的模型映射不一致)

如果改动非常大,那么可能就要换另外一种方案了,数据迁移,或者自己导入数据。