VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之C# 动态创建数据库三(MySQL)(3)

  

添加SchoolContext 继承DbContext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
    public class SchoolContext : DbContext
    {
        public SchoolContext()
            base("name=conncodefirst")
        {
 
        }
 
        static SchoolContext()
        {
            DbConfiguration.SetConfiguration(new MySql.Data.Entity.MySqlEFConfiguration());
        }
 
        public DbSet<Student> Students { getset; }
        public DbSet<Standard> Standards { getset; }
 
        //protected override void OnModelCreating(DbModelBuilder modelBuilder)
        //{
        //    base.OnModelCreating(modelBuilder);
        //}
    }

  

最后调用

1
2
3
4
5
6
SchoolContext dbCOntext = new SchoolContext();
                //dbCOntext.Database.
                bool flag = dbCOntext.Database.CreateIfNotExists();
                Student stud = new Student() { StudentName = "New Student", AddTime = DateTime.Now };
                dbCOntext.Students.Add(stud);
                dbCOntext.SaveChanges();

相关教程