EF初始化mysql数据库codefirst
EF初始化mysql数据库codefirst
1、添加引用EntityFramework、EntityFramework.SqlServer、MySql.Data、MySql.data.Entity.ef6
或者用NuGet安装EntityFramework、MySql.Data.Entity
2、在配置文件中加入
<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 codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
<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>
<connectionStrings>
<add name="MySql" connectionString="Server=localhost; Database=wx.mms; Uid=root; Pwd=root; " providerName="MySql.Data.MySqlClient" />
</connectionStrings>
3、新建UserContext类,此类继承DbContext
public class UserDBContext:DbContext { public UserDBContext() : base("name=MySql") { } ////去掉表名复数 //protected override void OnModelCreating(DbModelBuilder modelbuilder) //{ // modelbuilder.Conventions.Remove<PluralizingTableNameConvention>(); //} public DbSet<model.user> User { get; set; } }
4、System.Data.Entity;
//在没有数据库时创建一个,已存在,默认;如果pcontext.Patients.Add(p);添加一条,如果数据库已存在报错 Database.SetInitializer(new CreateDatabaseIfNotExists<UserDBContext>()); //在模型改变时,自动重新创建新的数据库,在开发过程中使用 //Database.SetInitializer(new DropCreateDatabaseIfModelChanges<UserDBContext>());
//每次运行时,都重新生成数据库
//Database.SetInitializer(new DropCreateDatabaseAlways<UserDBContext>());
using (UserDBContext udb=new UserDBContext())
{
//判断数据库是不是已经存在
if (!context.Database.Exists())
{
//如果数据库已存在不加Database.SetInitializer(new CreateDatabaseIfNotExists<PatientContext>());,会报错
udb.Database.Initialize(true);
//直接添加一条数据,会生成数据库和数据,
//udb.Patients.Add(u);
udb.SaveChanges();
}
}
5、在那个项目里面运行,就在那个项目里加上面的配置文件,安装EntityFramework、EntityFramework.SqlServer、MySql.Data、MySql.data.Entity
可以把上面的操作写在、、方法里、、直接在global中调用,程序启动时初始化数据库。
6、还可以同时把数据也初始化,
public class DataInitUser : IDataInitializer<mmsDbContext> { public void Initialize(mmsDbContext context) { var users = new List<muser>() { new muser(){name="张三", user_id=123, org_id=123456789, phone_no="1234567890123", otype=operator_type.admin, enabled=true} }; users.ForEach(u=>context.user.Add(u)); } }
初始化方法
public class mmsDataBuilder { public void Build() { Database.SetInitializer(new CreateDatabaseIfNotExists<mmsDbContext>()); using (var context = new mmsDbContext()) { if (!context.Database.Exists()) { new List<IDataInitializer<mmsDbContext>>() { new DataInitUser() 可以添加多个实体类 }.Setup<mmsDbContext>(new mmsDbContext()); context.SaveChanges(); } } } }