1.新建Mvc5项目,更改身份验证为:不进行身份验证,由于使用到webapi,勾选webapi选项
2.安装Entity Framework
3.安装mysql connector 地址:http://dev.mysql.com/downloads/connector/net/
安装mysql-for-visualstudio 地址: http://dev.mysql.com/downloads/windows/visualstudio/
这样可以在vs的 服务器资源管理器中直接添加MysqlDatabase的连接
4.在Mvc项目中引用Mysql
5.在models文件夹下新建3个model :Course、Enrollment、Student
public class Course { [DatabaseGenerated(DatabaseGeneratedOption.None)] public int CourseID { get; set; } public string Title { get; set; } public int Credits { get; set; } public virtual ICollectionEnrollments { get; set; } }
public enum Grade { A, B, C, D, F } public class Enrollment { public int EnrollmentID { get; set; } public int CourseID { get; set; } public int StudentID { get; set; } public Grade? Grade { get; set; } public virtual Course Course { get; set; } public virtual Student Student { get; set; } }
public class Student { public int ID { get; set; } public string LastName { get; set; } public string FirstMidName { get; set; } public DateTime EnrollmentDate { get; set; } public virtual ICollectionEnrollments { get; set; } }
6.创建数据上下文:新建文件夹DAL,添加类:DemoDbContext
[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))] public class DemoDbContext : DbContext { static DemoDbContext() { Database.SetInitializer(new DropCreateDatabaseIfModelChanges()); } public DemoDbContext() : base("EFContext") { } public DbSet Students { get; set; } public DbSet Enrollments { get; set; } public DbSet Courses { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove (); } }
[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))] 如果去掉这一句,EF自动创建数据库时会报错,而此时创建控制器又会报错,所以创建控制器的时候注销这句就可以了
modelBuilder.Conventions.Remove
Database.SetInitializer(new DropCreateDatabaseIfModelChanges
构造函数数中的DbConnectionString 是存在webconfig中的数据库连接字符串
"EFContext" connectionString="server=192.168.0.183;user id=root;password=w123456;persistsecurityinfo=True;database=MvcDemo" providerName="MySql.Data.MySqlClient" />
7.开发环境中,如果数据结构发生变化,需要重新建库,每次建库后要重新插入测试数据,可以用DropCreateDatabaseIfModelChanges类来实现(生成环境中请使用 Migrations做数据迁移),在DAL文件夹中新建类:DbInitializer
"MvcDemo.DAL.DemoDbContext,MvcDemo" disableDatabaseInitialization="true"> "MvcDemo.DAL.DbInitializer,MvcDemo"> "System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> "System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> "MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d">
disableDatabaseInitialization="true" 可以禁用数据初始化
配置webconfig
"MvcDemo.DAL.DemoDbContext,MvcDemo"> "MvcDemo.DAL.DbInitializer,MvcDemo"> "System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> "System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> "MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d">
运行结果:
8.添加一个StudentController,先生成一次项目,然后添加控制器
9.在_layout.cshtml添加链接
至此一个简单MVC EF Codefirst 访问Mysql 的项目就完成了,后面讲介绍如何在开发环境和生产环境修改数据库结构
参考:
使用MVC5的EF6 Code First入门系列:建立一个EF数据模型
Win10+VS2015+EF6.0+MySQL5.6+MVC环境部署和排错