日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

在VisualStadio2015上使用EF6.0建立MySql数据库

發(fā)布時(shí)間:2023/11/29 数据库 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在VisualStadio2015上使用EF6.0建立MySql数据库 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.新建工程

2.建立類的文件夾DAL

3.建立相關(guān)類

【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.調(diào)整配置文件 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 測試建立數(shù)據(jù)庫

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.測試成功,打開數(shù)據(jù)庫,可以見到mysql里增加一個(gè)庫test ,里面有一張表student

?

8 數(shù)據(jù)表中的字段發(fā)生變化情況處理

如果變動(dòng)比較小,比如新增或刪除一兩個(gè)字段
先備份好數(shù)據(jù)庫以后?直接進(jìn)行數(shù)據(jù)表操作,將你的數(shù)據(jù)模型和數(shù)據(jù)庫表做上對(duì)應(yīng)就好了,
需要注意的是,項(xiàng)目上線一般需要將DataContext設(shè)置一下
Database.SetInitializer<DataContext>(null);
而不能設(shè)置
Database.SetInitializer<DataContext>(new?DropCreateDatabaseIfModelChanges<DataContext>());或者
Database.SetInitializer<DataContext>(new?DropCreateDatabaseAlways<DataContext>());等等其他方式,
以防意外導(dǎo)致數(shù)據(jù)庫被刪除,重新生成(因?yàn)樽约禾砑幼侄慰赡芘c你的模型映射不一致)

如果改動(dòng)非常大,那么可能就要換另外一種方案了,數(shù)據(jù)遷移,或者自己導(dǎo)入數(shù)據(jù)。

?

轉(zhuǎn)載于:https://www.cnblogs.com/meetweb/p/5572005.html

總結(jié)

以上是生活随笔為你收集整理的在VisualStadio2015上使用EF6.0建立MySql数据库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。