标签:database tin count 模式 添加 oss student 结构 数据库
Code First又称代码优先,EF提供类型结构生成SQL并创建数据库中的表
student类
public class student:DbContext 注:DbContext表示一个工作单元和存储库的组合模式,可以用来从数据库查询和组一起变化。
{
public student()
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<student, DbConfiguration>()); 注:初始化自动迁移
}
public DbSet<message> message { get; set; } 注:默认生成的表的类型为复数形式
}
public class DbConfiguration : DbMigrationsConfiguration<student>
{
public DbConfiguration()
{
AutomaticMigrationsEnabled = true; 注:开启自动迁移
AutomaticMigrationDataLossAllowed = true; 注:迁移的时候是否允许数据丢失
}
}
message类
public class message
{
public int messageID 注:当类名加ID时自动设为主键
{
get;
set;
}
public string stuName
{
get;
set;
}
public string stuSex
{
get;
set;
}
public int stuAge
{
get;
set;
}
public long stuPhone
{
get;
set;
}
}
调用 注:如果你写完不调用就不会创建数据库
student stu = new student();
public ActionResult Index()
{
if (stu.message.Count() == 0) 注:当stu.message.Count()的数据等于零的时候就向stu.message添加数据
{
stu.message.Add(new message() { stuAge = 10, stuName = "张三", stuSex = "男", stuPhone = 12345678912 });
stu.message.Add(new message() { stuAge = 11, stuName = "李四", stuSex = "女", stuPhone = 12145128912 });
stu.message.Add(new message() { stuAge = 12, stuName = "王五", stuSex = "女", stuPhone = 12345742912 });
stu.message.Add(new message() { stuAge = 13, stuName = "赵六", stuSex = "男", stuPhone = 12445678912 });
stu.SaveChanges();
}
}
标签:database tin count 模式 添加 oss student 结构 数据库
原文地址:http://www.cnblogs.com/zhang1999/p/7456443.html